【问题标题】:How to set different navigationbar color for different UIViewcontroller in xib?如何在xib中为不同的UIViewcontroller设置不同的导航栏颜色?
【发布时间】:2015-02-07 15:22:07
【问题描述】:

我试图在项目中为不同的 UIViewController 设置不同的 UINavigationBar 颜色。直到现在我在每个 UIViewController 类的 ViewdidAppear 方法中尝试了以下代码,但它仍然无法正常工作,导航栏的颜色没有改变。

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

请帮帮我

【问题讨论】:

  • 在 viewWillAppear 中更改导航栏颜色。

标签: ios objective-c uiviewcontroller uinavigationbar uicolor


【解决方案1】:

试试

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

或者,在 iOS 6 上,

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

如果您有一个导航控制器作为 rootViewController,请使用:

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

然后设置颜色:

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

如果你想在每个viewcontroller中改变颜色,只需将代码放在每个viewWillAppear方法中

如果您不想在每个视图控制器中覆盖viewWillAppear,您可以为您的项目创建一个超级视图控制器。但如果为时已晚,您也可以创建自定义 UINavigationController 并简单地覆盖 push/pop 方法,例如:

-(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 中的图像?是这个原因吗?
  • 有没有办法设置默认导航栏颜色,然后只在几个 UIViewControllers 中更改它?我尝试在 AppDelegate 中设置默认的蓝色,然后在我的一个 ViewController 中将颜色设置为红色......问题是当我返回时,导航栏保持红色......我希望它返回蓝色。有什么想法吗?
  • 您提到使用 viewWillAppear。这将需要对每个 UIViewController 进行更改......或为所有 UIViewController 使用基类。有没有办法创建默认值的概念,然后只在少数特殊情况下覆盖它?
  • 我曾经在我的应用程序中有一个超级视图控制器来管理这类事情(你在它的视图中设置正确的颜色,并且只在你想要的时候覆盖)。但您也可以覆盖导航控制器,请参阅我的新更新。
  • 谢谢。它的工作愉快!
【解决方案2】:

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

然后,打开属性检查

尝试不同的条形色调或更改其背面图像会起作用。

【讨论】:

  • 我的错误我想在xib中做
【解决方案3】:

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

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

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

【讨论】:

    【解决方案4】:
    1. 在预览控制器中实现使导航栏颜色清晰:

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

    【讨论】:

      【解决方案5】:

      斯威夫特:

      全球:

      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 颜色属性,不能传递整数
      • @lan Bell,这是我自己添加的一种方法,但对这个问题没有伤害:)
      • 谢谢!这段代码对我来说效果很好 - 但我建议这个例子类似于“self.navigationController?.navigationBar.barTintColor = .red”而不是个人使用自定义方法。
      【解决方案6】:

      斯威夫特 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()]
              }
      

      【讨论】:

      • 最好添加一些解释你如何解决OP的问题。
      【解决方案7】:

      当您在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()
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-13
        • 1970-01-01
        • 1970-01-01
        • 2019-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多