繁体   English   中英

NavigationBar 为一个 View 设置 TitleColor

[英]NavigationBar set TitleColor for one View

我面临着快速设置一个 ViewController 标题的字体颜色并在它消失时重置它的问题。 目前我可以将颜色从黑色设置为白色:

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
}

当我尝试用 UIColor.black 重置时,它不会改变任何东西。

当我尝试设置整个外观时,根本没有任何变化。

override func viewDidLoad() {
  UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
}


override func viewWillDisappear(_ animated: Bool) {
  UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.black]
}

我怎样才能做到这一点? 使用 Xcode 10.0 Swift 4

不是将代码添加到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
}

谢谢大家的帮助。

我用另一个功能让它为我工作:

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
}

当视图正在构建时也会调用此函数。 该解决方案对我有用,并且在显示视图时已经设置了颜色。
我对赞成/反对对此持开放态度。

您可以在任何视图的代码中使用此函数 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")
 }

子类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)
    }
}

如果MyCustomViewController是导航控制器的根,则在viewDidLoad设置它的初始标题颜色:

class MyCustomViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.yellow]
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM