Ios 更改TitleTextAttributes文本颜色取决于导航栏颜色
我有两个不同导航栏颜色的控制器。假设我想用导航栏黑色和TitleTextAttributes颜色白色设置第一个控制器,在导航到第二个控制器后,它会更改导航栏白色和TitleTextAttributes黑色。导航栏颜色更改,但标题文本属性未更改。请建议 我写了这段代码并用在viewwillappearch方法中 内部导航栏类别Ios 更改TitleTextAttributes文本颜色取决于导航栏颜色,ios,uinavigationbar,Ios,Uinavigationbar,我有两个不同导航栏颜色的控制器。假设我想用导航栏黑色和TitleTextAttributes颜色白色设置第一个控制器,在导航到第二个控制器后,它会更改导航栏白色和TitleTextAttributes黑色。导航栏颜色更改,但标题文本属性未更改。请建议 我写了这段代码并用在viewwillappearch方法中 内部导航栏类别 typedef NS_ENUM( NSUInteger, UINavigationBarColor) { White, Black }; +(void)s
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];
}
}
您的代码正在设置
UINavgiationBar外观上的tintColor
和titleTextAttributes
。这仅影响新创建的导航栏。它不会更改任何现有的导航栏
您需要将setNavigationColor
设置为实例方法,并将[self-appearance]
更改为仅self
错误,对于选择器“setTitleTextAttributes:”没有已知的类方法,正如我所说的,将其更改为实例方法(+
->-
)。