ios - 更改 TitleTextAttributes 文本颜色取决于导航栏颜色
问题描述
我有两个具有不同导航栏颜色的控制器。假设我想用导航栏黑色和 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];
}
}
解决方案
您的代码正在设置tintColor
and titleTextAttributes
on UINavgiationBar appearance
。这只影响新创建的导航栏。它不会更改任何现有的导航栏。
您需要创建setNavigationColor
一个实例方法并更改[self appearance]
为 just self
。
推荐阅读
- r - 使用正则表达式删除多个实例,但不删除实例之间的文本
- ios - 从单独的 Swift 框架/模块中使用 Apple Pay
- sql - 在基于条件的递归 SQL 查询中返回真/假
- mysql - 在重复的情况下更新。在第一次的情况下插入
- javascript - 打字稿:为枚举中的键添加类型
- kdb - 如何在 KDB 中生成对角矩阵?
- amazon-cloudwatch - Cloudwatch 日志洞察 sum() 在没有日志时设置默认值 0
- python - TypeError:“DataFrame”对象不能在 Python 中使用 Pandas 调用
- jquery - 如何在 jQuery 中使用索引和长度?
- angular - 如何将数据从 Angular 前端传递到 Spring Boot 后端?