简体   繁体   中英

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. 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.

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

Instead of adding the code to viewDidLoad() , add it into viewDidAppear(_:) , ie

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

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:

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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