[英]NavigationBar set TitleColor for one View
im facing the problem of setting the font-color of the title of one ViewController in swift and resetting it when it disappears.我面临着快速设置一个 ViewController 标题的字体颜色并在它消失时重置它的问题。 Currently I'm able to set the color from black to white with:
目前我可以将颜色从黑色设置为白色:
override func viewDidLoad() {
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
override func viewWillDisappear(_ animated: Bool) {
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
when I try resetting with UIColor.black it doesn't change anything.当我尝试用 UIColor.black 重置时,它不会改变任何东西。
when I try to set the whole appearance there is no change at all.当我尝试设置整个外观时,根本没有任何变化。
override func viewDidLoad() {
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
}
override func viewWillDisappear(_ animated: Bool) {
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.black]
}
How can I still achieve this?我怎样才能做到这一点? Using Xcode 10.0 Swift 4
使用 Xcode 10.0 Swift 4
Instead of adding the code to viewDidLoad()
, add it into viewDidAppear(_:)
, ie不是将代码添加到
viewDidLoad()
,而是将其添加到viewDidAppear(_:)
,即
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
Thanks everybody for your help.谢谢大家的帮助。
I got it working for me with another function:我用另一个功能让它为我工作:
override func willMove(toParentViewController parent: UIViewController?) {
var textAttributes: [NSAttributedString.Key : Any]?
if parent == nil{ // navigating back
textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.black]
}else{
textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
}
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
This functions is also called when a view is building up.当视图正在构建时也会调用此函数。 This solution worked for me and the colors are already set when the view is displayed.
该解决方案对我有用,并且在显示视图时已经设置了颜色。
I'm open for pro/cons responds to this.我对赞成/反对对此持开放态度。
you can use this function viewWillAppear in your code on any view您可以在任何视图的代码中使用此函数 viewWillAppear
func navigationBarProperties(vc:UIViewController, title:String){
vc.navigationController!.navigationBar.isHidden = false
vc.navigationController!.navigationBar.isTranslucent = false
// text color
vc.navigationController!.navigationBar.tintColor = UIColor.white
// bar color
vc.navigationController!.navigationBar.barTintColor = UIColor.black
let isFont = UIFont.init(name: "Helvetica-bold", size: 15)
vc.navigationItem.title = title
vc.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) ,NSAttributedStringKey.font: isFont!]
}
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//---nav bar customization----//
navigationBarProperties(vc: self, title: "Home")
}
Subclass UINavigationController
and assign it to your navigationController:子类
UINavigationController
并将其分配给您的 navigationController:
class CustomNavigationController : UINavigationController {
let usualTextAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.red]
let customTextAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.blue]
private func updateTitleColorIfNeeded() {
if topViewController is MyCustomViewController {
navigationBar.titleTextAttributes = customTextAttributes
} else {
navigationBar.titleTextAttributes = usualTextAttributes
}
}
override func popViewController(animated: Bool) -> UIViewController? {
updateTitleColorIfNeeded()
return super.popViewController(animated: animated)
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
updateTitleColorIfNeeded()
super.pushViewController(viewController, animated: animated)
}
}
If MyCustomViewController
is root of the navigation controller, then set it's initial title color in viewDidLoad
:如果
MyCustomViewController
是导航控制器的根,则在viewDidLoad
设置它的初始标题颜色:
class MyCustomViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.yellow]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.