如何在iOS 11中为大型 Headers NavigationBar设置自定义背景图像?我正在使用我已分配给故事板中的navigationControllers的自定义子类 .
这是我创建自定义NavBar的方法:
class CustomNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationBar.tintColor = UIColor(red:1, green:1, blue:1, alpha:0.6)
self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
if #available(iOS 11.0, *) {
self.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .automatic
self.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
self.navigationBar.barTintColor = UIColor.green
}
self.navigationBar.isTranslucent = false
self.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "navigationBarBackground"), for: .default)
self.navigationBar.shadowImage = #imageLiteral(resourceName: "navigationBarShadow")
}
}
奇怪的是 setBackgroundImage(image, for: .default)
不适用于大型游戏 . 以前在iOS 10上工作过,如果我旋转iPhone(并激活小型NavBar)背景又回来了吗?
Edit: backgroundImage
仍然呈现但不知何故被隐藏 . 仅当您开始滚动并出现"normal"导航栏时,才会看到backgroundImage . 在这种情况下, barTintColor
也被完全忽略 .
6 回答
我有同样的问题,修复它
删除setBackgroundImage并将barTint颜色与模式图像一起使用
使用渐变颜色获取图像
在iOS 11中,如果使用大型 Headers ,则不再需要设置BackgroundImage(删除其声明) . 相反,你需要使用BarTintColor .
试试这段代码(Swift 4.0):
在viewDidLoad()中
在Xamarin中它会是这样的:
this.View.Layer.Insert是可选的 . 当我在NavigationBar上的图像上下来时,我需要它
捎带对oldrinmendez的回答 - 该解决方案适用于水平渐变 .
对于VERTICAL渐变,我可以通过在scrollViewDidScroll中再次调用它来使用oldrinmendez的答案中的相同函数 . 当用户滚动时,这会不断调整渐变图像的高度 .
Start with the function from oldrinmendez :
Create an update function to call it with the options you want:
Finally add the update function to scrollViewDidScroll & ViewDidApper: 使用ViewDidAppear以返回正确的导航栏高度
更改barTint对我没有用,所以我更改了navigationBar中的图层