How to set different navigationbar color for diffe

2020-08-18 15:28发布

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

7条回答
放我归山
2楼-- · 2020-08-18 15:40
  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.

查看更多
Animai°情兽
3楼-- · 2020-08-18 15:43

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)
}
查看更多
beautiful°
4楼-- · 2020-08-18 15:50

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()]
        }
查看更多
劳资没心,怎么记你
5楼-- · 2020-08-18 15:53

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.

查看更多
Melony?
6楼-- · 2020-08-18 15:55

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

查看更多
\"骚年 ilove
7楼-- · 2020-08-18 15:56

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

enter image description here

and then, open the attribute inspect,

enter image description here

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

查看更多
登录 后发表回答