简体   繁体   English

导航栏的颜色不会变回吗? IOS 7

[英]Navigation Bar color doesn't change back? iOS7

I have several tableviews with navigation controller. 我有几个带有导航控制器的表视图。 The navigation bar is white for all the views except the last one, which is red. 除最后一个视图(红色)外,所有视图的导航栏均为白色。 I have 我有

[[[self navigationController] navigationBar] setTintColor:[UIColor redColor];

in the viewDidLoad of the last tableview and 在最后一个表格视图的viewDidLoad中

[[[self navigationController] navigationBar] setTintColor:[UIColor whiteColor];

in the viewDidLoad of other table views. 在其他表视图的viewDidLoad中。

It works fine until I get to the last view and the color of the navigation bar changes to red. 直到我到达最后一个视图并且导航栏的颜色变为红色,它才能正常工作。 When I go back to the previous views, all the previous views' navigation bar has changed to red. 当我返回以前的视图时,所有以前的视图的导航栏都变为红色。 Where should I put those codes so the color will remain unchanged? 我应该在哪里放置这些代码,以便颜色保持不变?

Thanks, 谢谢,

Put that in viewWillAppear: . 将其放在viewWillAppear: Code in this method will be run every time the view reappear. 每次重新显示视图时,都会运行此方法中的代码。

Use viewDidLoad for initialization purposes only as it is called one time. 仅在一次调用时将viewDidLoad用于初始化目的。 Adding your code in viewWillAppear method should fix your problem as it gets called every time the view appears. 将代码添加到viewWillAppear方法中可以解决您的问题,因为每次视图出现时都会被调用。

However, I like using the UIAppearance to customize the appearance of all instances of a class by creating another method - 但是,我喜欢使用UIAppearance通过创建另一个方法UIAppearance定义类的所有实例的外观-

-(void) setupAppearance {

    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];

    navigationBarAppearance.barTintColor = [UIColor redColor];

    // Another method 
    // [UINavigationBar appearance].barTintColor = [UIColor redColor];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM