Iam trying to set different UINavigationBar color for different UIViewController in project.Till now i tried following code in ViewdidAppear methode of each UIViewController class and its not working still the color of navigationbar is not changing.
UIImage *navBackgroundImage = [UIImage imageNamed:@"redbar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
Please help me
From Settings, tap Display, and then tap Navigation bar. Make sure Buttons is selected, and then you can choose your desired button setup at the bottom of the screen. Note: This option will also affect the location you swipe when using Swipe gestures.
To change a navigation bar color in SwiftUI, you apply toolbarBackground modifier to the content view of NavigationStack . NavigationView is deprecated in iOS 16. toolbarBackground accepts two parameters. ShapeStyle : The style to display as the background of the bar.
Try
[[UINavigationBar appearance] setBarTintColor: [UIColor redColor]];
Or, on iOS 6,
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
If you have a navigation controller as rootViewController, get it with:
UINavigationController* nc = (UINavigationController*)[[[UIApplication sharedApplication] delegate] window].rootViewController;
And then set the color:
[nc.navigationBar setBarTintColor:[UIColor redColor]];
And if you want to change the color in each viewcontroller, just put the code in each viewWillAppear
method
If you don't want to override the viewWillAppear
in every viewcontroller, you can create a super viewcontroller for your project. But if it's too late, you can also create a custom UINavigationController
and simply override push/pop methods like:
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
[super pushViewController:viewController animated:animated];
[self.navigationBar setBarTintColor:[UIColor redColor]];
}
Do this for the four methods:
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
Or at least for the methods you use.
And then override viewWillAppear
in your viewControllers which need another bar color.
Most of the solutions will give you a blink effect while changing background color as you are setting colors on viewWillAppear
.
A solution without having blink effect is by setting the navigation bar color for each view controller in viewDidLoad
and then changing the background color before navigation as below.
override func viewDidLoad() {
super.viewDidLoad()
self.showGrayNavigationBar()
}
And then find the previous view controller and set the background color before viewAppears as :
override func willMove(toParentViewController parent: UIViewController?) {
let count = self.navigationController?.viewControllers.count
let vc = self.navigationController?.viewControllers[count! - 2]
if vc is ServicesViewController{
vc!.showGrayNavigationBar()
}else if vc is ServiceDetailsViewController{
vc!.showBlueNavigationBar()
}
}
Swift:
Global:
self.navigationController?.navigationBar.tintColor = UIColor.RGB(197, 104, 66)
For each UIViewController:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = UIColor.RGB(197, 104, 66)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With