Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set different navigationbar color for different UIViewcontroller in xib?

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

like image 915
Naveen Kumar Avatar asked Dec 09 '14 16:12

Naveen Kumar


People also ask

How do I customize my navigation bar?

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.

How do I change the navigation bar color in SwiftUI?

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.


3 Answers

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

like image 69
Y.Bonafons Avatar answered Nov 02 '22 22:11

Y.Bonafons


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()
    }
}
like image 28
Anish Parajuli 웃 Avatar answered Nov 02 '22 23:11

Anish Parajuli 웃


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)
}
like image 31
William Hu Avatar answered Nov 02 '22 22:11

William Hu