0

Why doesn't the navigationBar title change its colour to white when I'm back on my main UIViewController? Here is simple code (viewWillAppear, viewWillDisappear), but it doesn't work, the title stays green when I'm back on this VC. The main colour in app is green too:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIApplication.shared.statusBarStyle = .lightContent

    DispatchQueue.main.async {
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name: "Gotham-Medium", size: 20)!]
    }        
}


override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.statusBarStyle = .default

    navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.green, NSAttributedStringKey.font: UIFont(name: "Gotham-Medium", size: 20)!]


}
dirkgroten
  • 20,112
  • 2
  • 29
  • 42
Dejan Malbasic
  • 278
  • 3
  • 10

3 Answers3

1
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIApplication.shared.statusBarStyle = .lightContent

    DispatchQueue.main.async {
       addTitleLabel()
    }        
}


override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.statusBarStyle = .default
}

func addTitleLabel(){
   var titleLabel: UILabel = UILabel()
   titleLabel.textColor = .white
   titleLabel.textAlignment = .center
   titleLabel.text = "Home"

  self.navigationItem.titleView = titleLabel
}

Call this method form viewWillAppear.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
  • Nope, that only give me blue back button in the second controller – Dejan Malbasic Jan 13 '18 at 20:40
  • Thanks Faysal. I know all of this navigation bar settings. I set all this in view did load, and when i start app for the first time title in nav bar is white and good, but when i'm using app, every time when i'm back on the main controller title is green :/ on all others controllers i'm using only view will appear, nothing in view did load... – Dejan Malbasic Jan 13 '18 at 21:14
  • Also for main VC, you need to set the color on ViewWillAppear. Otherwise, color does not change when back to the main VC. – Faysal Ahmed Jan 13 '18 at 21:23
  • Yes. This code in the first post here is source from the main VC. – Dejan Malbasic Jan 13 '18 at 21:30
  • Try to remove this code form ViewWillDissAppear and excute. `navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.green, NSAttributedStringKey.font: UIFont(name: "Gotham-Medium", size: 20)!]` Please let me know what will happen. – Faysal Ahmed Jan 13 '18 at 21:55
  • Yes, that is logical Ahmed. I did it, again, and everything working ok until i'm click < back on main, again green title. in the second controller i only have same 'green code' in view will appear, nothing more... not working. i set debugger on the white view will appear, on the main CV, and code stoped nicely... but still green.. i'm not sure what to do next. Thank you – Dejan Malbasic Jan 13 '18 at 22:31
  • did you get any solution? – Ilesh P Apr 30 '18 at 05:31
1

This isn't going to work how you want because of how the navigation bar is shared between the view controllers and how the system updates it.

What you can do however is put a UILabel in the title of the navigation bar in the font and colour you want. The advantage of this method is that the UILabel only applies to that particular view controller so you never need to reset it.

So in the viewDidLoad of the pushed (second) view controller put this code:

let label = UILabel()
label.font = UIFont(name: "Menlo", size: 20)
label.textColor = .white
label.text = self.navigationItem.title
self.navigationItem.titleView = label

(note you can set the text to anything you want but self.navigationItem.title keeps it simple)

You can now remove the code related to the navigation bar from the viewWillAppear and viewWillDisappear methods.

Upholder Of Truth
  • 4,643
  • 2
  • 13
  • 23
  • Cool glad it worked. It's also a bit simpler to handle as once set you don't have to worry about clearing it. – Upholder Of Truth Jan 13 '18 at 22:40
  • If it works as you want can you accept the answer. Not just because I gain reputation but because it also allows others with the same question to see the correct answer. – Upholder Of Truth Jan 13 '18 at 22:59
0

You’ve to add navigation title color code in view will appear of previous view controller.