Problem with barTintColor of NavigationBar and XCode 11.4

Hello !


I'm facing a problem since I updated XCode to 11.4 version.
Since the update, when I change the navigationBar barTintColor programmatically, the new color is not displayed ! The code was working on XCode 11.3 and still works for iPhones on iOS 12, so, I don't think this is a code problem.


Is somebody else having the same problem ?

Answered by nurider in 411591022

Yes, having he same problem. This fixed it for me:


// Customize the navigation controller

if (@available(iOS 13, *)) {

self.navigationBar.standardAppearance = [[UINavigationBarAppearance alloc] init];

[self.navigationBar.standardAppearance configureWithDefaultBackground];

self.navigationBar.standardAppearance.backgroundColor = [Branding shared].primaryColor;

self.navigationBar.standardAppearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};

}

else {

self.navigationBar.barTintColor = [Branding shared].primaryColor;

self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};

}

self.navigationBar.tintColor = [UIColor whiteColor];

self.navigationBar.translucent = NO;

Accepted Answer

Yes, having he same problem. This fixed it for me:


// Customize the navigation controller

if (@available(iOS 13, *)) {

self.navigationBar.standardAppearance = [[UINavigationBarAppearance alloc] init];

[self.navigationBar.standardAppearance configureWithDefaultBackground];

self.navigationBar.standardAppearance.backgroundColor = [Branding shared].primaryColor;

self.navigationBar.standardAppearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};

}

else {

self.navigationBar.barTintColor = [Branding shared].primaryColor;

self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};

}

self.navigationBar.tintColor = [UIColor whiteColor];

self.navigationBar.translucent = NO;

Oh! Thank you ! It works !!

Is there a way to impliment this universally say from the appdelegate so as not to copy the code to every viewcontroller class? I have set up a class and method from ViewDidLoad, but even with that I'll still have to change every view controller.


Thanks,

On the storyboard, for your Navigation Controller change the "Bar Tint" to its "Default" value, then on your code you can change it as you normally would.

The "correct answer" didn't work for me because I'm using UINavigationBar.appearance().

Your solution was perfect.

I set every color value of my storyboard to "default" and every bar was fine. Thank you so much !

Swift global:

extension UINavigationController{
  open override func viewDidLoad() {
    super.viewDidLoad()
    if #available(iOS 13, *) {
      self.navigationBar.standardAppearance = UINavigationBarAppearance()
      self.navigationBar.standardAppearance.configureWithDefaultBackground()
      self.navigationBar.standardAppearance.backgroundColor = mainColor
      self.navigationBar.standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    }
    else {
      self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
      self.navigationBar.barTintColor = mainColor
    }
  }
}

Problem with barTintColor of NavigationBar and XCode 11.4
 
 
Q