![](/img/trans.png)
[英]how to change background color of the navigation bar and navigation text color
[英]Change TitleTextAttributes text color depends on navigation bar color
我有兩個具有不同導航欄顏色的控制器。 假設我想將第一個控制器設置為導航欄黑色,並將TitleTextAttributes顏色設置為whiteColor,然后導航至第二個控制器,它將更改導航欄白色和TitleTextAttributes黑色。 導航欄顏色已更改,但標題文本屬性未更改。 請提出建議。
我寫這段代碼,並在viewWillAppear方法中使用。
內部導航欄類別
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 appearance
上設置tintColor
和titleTextAttributes
。 這只會影響新創建的導航欄。 它不會更改任何現有的導航欄。
您需要將setNavigationColor
為實例方法,並將[self appearance]
更改為self
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.