[iOS微博项目 - 1.1] - 设置导航栏主题(统一样式)
2024-08-21 05:01:09
A.导航栏两侧文字按钮
1.需求:
所有导航栏两侧的文字式按钮统一样式
普通样式:橙色
高亮样式:红色
不可用样式:亮灰
阴影:不使用
字体大小:15
2.实现效果
默认样式:
统一使用样式:
3.思路
- 在创建item的时候逐个设置:代码超级冗余
- 抽取创建公共父类:稍好的选择,但是继承了此公共父类的控制器,就不能操作其去继承系统自带的控制器类了,造成很大的隐患。iOS中控制器不建议提取公共父类,最好直接继承系统自带控制器。
- 使用主题appearance统一设置所有UIBarButtonItem的样式:采用!在自定义的UINavigationController的类初始化方法中实现一次,就可以改变所有使用了此类的BarButtonItem样式
4.实现
HVWNavigationViewController.m:
/** 类初始化的时候调用 */
+ (void)initialize {
// 初始化导航栏样式
[self initNavigationBarTheme]; // 初始化导航栏item样式
[self initBarButtonItemTheme];
} /** 统一设置导航栏item的样式
* 因为是通过主题appearence统一修改所有NavivationBar的样式,可以使用类方法
*/
+ (void) initBarButtonItemTheme {
// 设置导航栏,修改所有UINavigationBar的样式
UIBarButtonItem *appearance = [UIBarButtonItem appearance]; // 设置noraml状态下的样式
NSMutableDictionary *normalTextAttr = [NSMutableDictionary dictionary];
// 字体大小
normalTextAttr[NSFontAttributeName] = [UIFont systemFontOfSize:];
// 字体颜色
normalTextAttr[NSForegroundColorAttributeName] = [UIColor orangeColor];
// 设置为normal样式
[appearance setTitleTextAttributes:normalTextAttr forState:UIControlStateNormal]; // 设置highlighted状态下的样式
NSMutableDictionary *highlightedTextAttr = [NSMutableDictionary dictionaryWithDictionary:normalTextAttr];
// 字体颜色
highlightedTextAttr[NSForegroundColorAttributeName] = [UIColor redColor];
// 设置为normal样式
[appearance setTitleTextAttributes:highlightedTextAttr forState:UIControlStateHighlighted]; // 设置disabled状态下的样式
NSMutableDictionary *disabledTextAttr = [NSMutableDictionary dictionaryWithDictionary:normalTextAttr];
// 字体颜色
disabledTextAttr[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
// 设置为normal样式
[appearance setTitleTextAttributes:disabledTextAttr forState:UIControlStateDisabled]; }
B.设置导航栏样式
1.需求:
- 统一显示文字颜色:黑色
- 文字阴影:禁止
- 字体大小:20
2.思路:同“A”
3.实现:
同“A"
HVWNavigationViewController.m:
/** 统一设置导航栏样式 */
+ (void) initNavigationBarTheme {
// 使用appearence(主题)设置,统一修改所有导航栏样式
UINavigationBar *appearance = [UINavigationBar appearance]; // 为了统一iOS6和iOS7,iOS6需要设置导航栏背景来模拟iOS7的效果
if (!iOS7) {
[appearance setBackgroundImage:[UIImage imageWithNamed:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault];
} // 设置属性
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
// 设置字体
attr[NSForegroundColorAttributeName] = [UIColor blackColor];
attr[NSFontAttributeName] = [UIFont systemFontOfSize:];
// 消去文字阴影,设置阴影偏移为0
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeZero;
attr[NSShadowAttributeName] = shadow; [appearance setTitleTextAttributes:attr];
}
最新文章
- Cassandra简介
- Mysql BLOB和TEXT类型
- 集锦.txt
- 解决Jquery对input file控件的onchange事件只生效一次的问题
- LA 3602 DNA Consensus String
- 如何清除Linux 登陆信息及日志
- 简单讲解iOS应用开发中的MD5加密的相关使用<;转>;
- mssql定时执行作业。
- json学习初体验--第三者jar包实现bean、List、map创json格式
- JQuery和JS操作LocalStorage/SessionStorage的方法
- Sublime Text3 运行Python 出现Error:Decode error - output not utf-8
- springboot中配置过滤器以及可能出现的问题
- CF1033G Chip Game
- 2019.03.26 bzoj4447: [Scoi2015]小凸解密码(线段树)
- 启用SQL Server 2014 中的OLE 自动化功能
- jquery实现一个标签图标hover到上面的时候显示tooltip
- target存放的是编译后的.class文件地方 默认情况下不会讲非class文件放入进入 如果要使用非.class文件 需要通过增加配置方式自动加入文件
- Data - Hadoop伪分布式配置 - 使用Hadoop2.8.0和Ubuntu16.04
- grunt-inline:一个资源内嵌插件
- 【题解】Catering World Finals 2015 上下界费用流
热门文章
- Android开发之Okhttp:java.lang.IllegalStateException: closed
- Android开发之权限列表
- IDEA 创建Web项目并在Tomcat中部署运行
- Android studio编译之后显示中文乱码的问题解决办法
- axis : java.lang.NoSuchMethodError
- if(username.equals(“zxx”){}
- 亲测!Jquery2.0不支持IE8-了
- 模仿 ";淘宝彩票"; 的随机选球投注效果!
- codevs 3123 高精度练习之超大整数乘法
- this class is not key value coding-compliant for the key ##