如何在xib中为不同的UIViewcontroller设置不同的导航栏颜色?

10

我试图在项目中为不同的UIViewController设置不同的UINavigationBar颜色。到目前为止,我尝试了在每个UIViewController类的ViewdidAppear方法中使用以下代码,但仍然无效,导航栏的颜色仍未更改。

UIImage *navBackgroundImage = [UIImage imageNamed:@"redbar.png"];
    [[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

请帮助我


在viewWillAppear中更改导航栏颜色。 - Vineesh TP
7个回答

12

尝试

[[UINavigationBar appearance] setBarTintColor: [UIColor redColor]];

或者,在 iOS 6 上,

[[UINavigationBar appearance] setTintColor:[UIColor redColor]];

如果你的根视图控制器是导航控制器,可以通过以下方式获取它:

    UINavigationController* nc = (UINavigationController*)[[[UIApplication sharedApplication] delegate] window].rootViewController;

接着设置颜色:

[nc.navigationBar setBarTintColor:[UIColor redColor]];

如果你想在每个视图控制器中更改颜色,只需将代码放入每个viewWillAppear方法中。

如果您不想在每个视图控制器中重写viewWillAppear,可以为项目创建一个超级视图控制器。但如果已经太晚了,您也可以创建一个自定义UINavigationController并简单地覆盖推送/弹出方法,例如:

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    [super pushViewController:viewController animated:animated];
    [self.navigationBar setBarTintColor:[UIColor redColor]];
}

对这四个方法都要执行此操作:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

或者至少适用于你使用的方法。 然后在需要另一种导航栏颜色的视图控制器中覆盖 viewWillAppear 方法。


无法工作,在我的主页中,我在UINavigationBar中使用了图像?这是原因吗? - Naveen Kumar
有没有办法设置默认的导航栏颜色,然后只在几个UIViewControllers中更改它?我尝试在AppDelegate中设置默认值为蓝色,然后在我的一个ViewControllers中将颜色设置为红色... 问题是当我返回时,导航栏仍然是红色... 我想让它回到蓝色。有什么建议吗? - SuperDeclarative
你提到了使用viewWillAppear。这将需要对每个UIViewController进行更改...或者使用一个基类来处理所有的UIViewControllers。难道没有一种方法可以创建默认值的概念,然后只在少数特殊情况下覆盖它吗? - SuperDeclarative
我曾经在我的应用程序中使用一个超级视图控制器来管理这种事情(您可以在其viewwillappear中设置正确的颜色,并仅在需要时进行覆盖)。但是,您也可以重写navigationcontroller,看看我的新更新。 - Y.Bonafons
只用了2秒就为我解决了问题。谢谢HeRo。 - Ravi

3

Swift:

Global:

self.navigationController?.navigationBar.tintColor = UIColor.RGB(197, 104, 66)

对于每个UIViewController:

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = UIColor.RGB(197, 104, 66)
}

没有RGB颜色属性,也不能传递整数。 - user5273262
1
@Ian Bell,这是我自己添加的一个方法,但对于这个问题并没有影响:) - William Hu
谢谢!这段代码对我很有效 - 但我个人建议示例应该是像“self.navigationController?.navigationBar.barTintColor = .red”这样的,而不是使用自定义方法。 - woody121

3
似乎您想在Storyboard中设置颜色,而不是在代码中。

enter image description here

接着,打开属性检查器

enter image description here

尝试不同的栏条颜色或更改其背景图像即可。

我的错误,我想在XIB中完成它。 - Naveen Kumar

1
大多数解决方案在更改背景颜色时会出现闪烁效果,因为您正在viewWillAppear上设置颜色。
一种不会出现闪烁效果的解决方案是,在viewDidLoad中为每个视图控制器设置导航栏颜色,然后在导航之前更改背景颜色,如下所示。
override func viewDidLoad() {
    super.viewDidLoad()
    self.showGrayNavigationBar()
}

然后查找上一个视图控制器,并在 viewAppears 之前设置背景色:

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

0

使用UINavigationController作为您的根视图,您必须在ViewWillAppear方法中使用以下方法:

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

这将会改变你当前的UIViewController导航栏颜色 :)


0

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

1
最好添加一些解释,说明您如何解决OP的问题。 - Janki Gadhiya

0
  1. 在预览控制器中实现使导航栏透明:

    -(void)viewdidload{
        [self.navigationController.navigationBar setBackgroundImage:[XXXImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
        ...
    }
    
  2. 将自定义视图设置在与导航栏相同的框架内。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接