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

问题描述 投票:9回答:7

我试图在项目中为不同的UIViewController设置不同的UINavigationBar颜色。现在我尝试在每个UIViewController类的ViewdidAppear方法中跟随代码并且它不工作仍然导航栏的颜色没有改变。

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

请帮我

ios objective-c uiviewcontroller uinavigationbar uicolor
7个回答
11
投票

尝试

[[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方法中

如果您不想在每个viewcontroller中覆盖viewWillAppearin,则可以为项目创建超级视图控制器。但如果为时已晚,您还可以创建自定义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;

或者至少对你使用的方法。然后在viewControllers中覆盖需要另一个条形颜色的viewWillAppear


3
投票

您似乎想要在故事板中设置颜色,而不是在代码中。

然后,打开属性inspect,

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


3
投票

迅速:

全球:

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

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
投票
  1. 在预览控制器中实现,使导航栏颜色清晰: -(void)viewdidload{ [self.navigationController.navigationBar setBackgroundImage:[XXXImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault]; ... }
  2. 在导航栏的同一框架中设置自定义视图。

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

0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.