简体   繁体   English

更改TitleTextAttributes文本颜色取决于导航栏颜色

[英]Change TitleTextAttributes text color depends on navigation bar color

I am having two controller with different navigation bar color. 我有两个具有不同导航栏颜色的控制器。 let say I want to set first controller with navigation bar black color and TitleTextAttributes color whiteColor and after navigate to second controller it change navigation bar white color and TitleTextAttributes black color. 假设我想将第一个控制器设置为导航栏黑色,并将TitleTextAttributes颜色设置为whiteColor,然后导航至第二个控制器,它将更改导航栏白色和TitleTextAttributes黑色。 navigation bar color change but title text attribute is not changing. 导航栏颜色已更改,但标题文本属性未更改。 Please suggest. 请提出建议。

i write this code and used in viewWillAppear method. 我写这段代码,并在viewWillAppear方法中使用。

Inside navigation bar category 内部导航栏类别

typedef NS_ENUM( NSUInteger, UINavigationBarColor) {
    White,
    Black
};

+(void)setNavigationColor:(UINavigationBarColor)color{
    if (color == White) {
         [[self appearance] setTintColor:[UIColor whiteColor]];
        NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor blackColor]};
        [[self appearance] setTitleTextAttributes:attributes];
    } else {
        [[self appearance] setTintColor:[UIColor blackColor]];
        NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
        [[self appearance] setTitleTextAttributes:attributes];
    }
}

Your code is setting the tintColor and titleTextAttributes on UINavgiationBar appearance . 您的代码正在UINavgiationBar appearance上设置tintColortitleTextAttributes That only affects newly created navigation bars. 这只会影响新创建的导航栏。 It does not change any existing navigation bars. 它不会更改任何现有的导航栏。

You need to make setNavigationColor an instance method and change [self appearance] to just self . 您需要将setNavigationColor为实例方法,并将[self appearance]更改为self

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

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