我正在尝试设置UINavigationBar外观(
tintColor
,
barTintColor
等等),但目前在iOS 11中,这一点似乎被完全忽视或表现不符合预期。当按下或弹出视图时,单个导航控制器内的条形图外观会发生变化。我有两个函数,我调用它们
viewWillAppear
.
我需要能够设置标题颜色、左右栏按钮项目颜色、后退按钮颜色和栏色调。
我正在努力让颜色在此刻发挥作用,所以我尝试了这个,但没有乐趣。
public func setDarkHeaderStyle() {
UIApplication.shared.statusBarStyle = .lightContent
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = Colours.secondaryNavy
UINavigationBar.appearance().isTranslucent = false
}
public func setLightHeaderStyle() {
UIApplication.shared.statusBarStyle = .default
UINavigationBar.appearance().tintColor = Colours.primaryNavy
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().isTranslucent = false
}
如果我改为使用导航控制器来设置颜色,它确实适用于条色调
UIBarButtonItem
和后退按钮,但标题不正确。
public func setDarkHeaderStyle() {
UIApplication.shared.statusBarStyle = .lightContent
navigationController?.navigationBar.tintColor = UIColor.white
navigationController?.navigationBar.barTintColor = Colours.secondaryNavy
navigationController?.navigationBar.isTranslucent = false
}
public func setLightHeaderStyle() {
UIApplication.shared.statusBarStyle = .default
navigationController?.navigationBar.tintColor = Colours.primaryNavy
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.isTranslucent = false
}
因此,我手动设置标题文本属性,如下所示:
public func setDarkHeaderStyle() {
UIApplication.shared.statusBarStyle = .lightContent
navigationController?.navigationBar.titleTextAttributes = [
NSAttributedStringKey.font: UIFont(name: Fonts.fontRegularName, size: 16)!,
NSAttributedStringKey.kern: 0.2,
NSAttributedStringKey.foregroundColor: UIColor.white
]
navigationController?.navigationBar.tintColor = UIColor.white
navigationController?.navigationBar.barTintColor = Colours.secondaryNavy
navigationController?.navigationBar.isTranslucent = false
}
public func setLightHeaderStyle() {
UIApplication.shared.statusBarStyle = .default
navigationController?.navigationBar.titleTextAttributes = [
NSAttributedStringKey.font: UIFont(name: Fonts.fontRegularName, size: 16)!,
NSAttributedStringKey.kern: 0.2,
NSAttributedStringKey.foregroundColor: Colours.primaryNavy
]
navigationController?.navigationBar.tintColor = Colours.primaryNavy
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.isTranslucent = false
}
这似乎奏效了,
除了
返回根视图时,标题颜色未设置:
我想我有两个问题:
为什么不
UINavigationBar.appearance()
工作
如何使其可靠工作?