-->

How to set different navigationbar color for diffe

2020-08-18 15:12发布

问题:

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

回答1:

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.



回答2:

It seems that you want to set color in storyboard, not in code.

and then, open the attribute inspect,

try different bar tint or change its back image will work.



回答3:

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)
}


回答4:

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()
    }
}


回答5:

  1. Implement in previews controller to make navigation bar clear color:

    -(void)viewdidload{
        [self.navigationController.navigationBar setBackgroundImage:[XXXImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
        ...
    }
    
  2. Set custom view in the same frame of navigation bar.



回答6:

Swift 2.2

 func navigationCtrl(){


            self.navigationController?.navigationBar.barTintColor = UIColor.redColor()

            self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "close_icon"), style: .Done, target: self ,action: #selector(UIViewController.dismissAction(_:)) )
            self.navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
            self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont.IRANSansWeb(UIFont.IRANSansWebType.Medium, size: 20) , NSForegroundColorAttributeName : UIColor.whiteColor()]
        }


回答7:

Using a UINavigationController as your root view, you have to use the following method on the ViewWillAppear method :

[self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];

It will change your current UIViewController navigationbar color :)