UIAppearance 基本使用
来源:互联网 发布:淘宝权微博 编辑:程序博客网 时间:2024/06/04 18:19
本文为博主记载文
在iOS 5以前,自定义原生控件的外观并没有原生支持,因此开发人员感觉很麻烦。开发时经常面临的问题是修改一个控件所有实例的外观。解决这个问题的正确方法是重写一遍控件。但由于这么做非常费时,一些开发人员开始覆盖或混写一些方法,如
drawRect:
从 iOS5 开始, 苹果通过两个协议(UIAppearance 和 UIAppearanceContainer)规范了对许多UIKit控件定制的支持。所有遵循 UIAppearance 协议的UI控件通过定制都可以呈现各种外观。不仅如此,UIAppearance 协议甚至允许开发者基于控件所属的区域指定不同的外观。也就是说,当某个控件包含在特定视图中时,可以指定它的外观(如 UIBarButtonItem 的 tintColor)。也可以获取该控件类的外观代理对象,用该代理定制外观来实现,下面来看几个例子。
例1 : 设置项目中所有的UILabel的font?
swift
UILabel.appearance().font = UIFont(name: "Avenir-Light", size: 17.0)
public static func appearance() -> Self // swift 系统方法原型
Objective-C
[UILabel appearance].font = [UIFont fontWithName:@"Avenir-Light" size:17.0];
+ (id)appearance // obj-c 系统方法原型
例2 : 把所有的UIButton的title都设成白色? 或者 所有的UIButton都有统一的背景图片?
swift
// 设置所有UIButton的title颜色UIButton.appearance().setTitleColor(UIColor.whiteColor(), forState: .Normal)// 设置所有UIButton的背景图UIButton.appearance().setBackgroundImage(UIImage(named: "图片"), forState: .Normal)
Objective-C
// 设置所有UIButton的title颜色[[UIButton appearance] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];// 设置所有UIButton的背景图[[UIButton appearance] setBackgroundImage:[UIImage imageNamed:@"图片"] forState:UIControlStateNormal];
例3 : 设置项目中所有 UITableViewCell 内的文本字体统一?
swift
// 这个方法就是找到 传入数组参数中所有的 UILabel (数组中存放的都是AnyClass)UILabel.appearanceWhenContainedInInstancesOfClasses([UITableViewCell.self]).font = UIFont(name: "Avenir-light", size: 14.0)
Objective-C
// 这个方法就是找到 传入数组参数中所有的 UILabel (数组中存放的都是AnyClass)[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITableViewCell class]]].font = [UIFont fontWithName:@"Avenir-light" size:14.0];
提醒 : 修改UIAppearance,有个限制,就是如果想让它生效,必须在下次装载入app的主窗口时才能生效,所以,如果要通过UIAppearance动态修改组件的风格,我们就需要在UIAppDelegate中实现下面的代码
1 0
- UIAppearance 基本使用
- UIAppearance的使用
- iOS UIAppearance使用详解
- iOS UIAppearance使用详解
- iOS UIAppearance使用详解
- iOS UIAppearance使用详解
- iOS中UIAppearance使用
- iOS UIAppearance使用详解
- iOS UIAppearance使用详解
- iOS UIAppearance使用详解
- UIAppearance的使用
- iOS UIAppearance使用详解
- UIAppearance的使用
- iOS UIAppearance使用详解
- iOS UIAppearance使用详解
- iOS UIAppearance使用详解
- iOS UIAppearance使用详解
- iOS UIAppearance使用详解
- 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3
- ContentProvider入门
- Android中跨进程通信方式之使用AIDL进阶篇
- OpenGL ES Shader相关API 总结【4】—— GLSL 语法小结【持续更新】
- RecyclerView 添加 Footer and Header
- UIAppearance 基本使用
- Android签名制作,keystore文件转换格式为pk8+x509.pem
- CSS强制设置样式
- Android关于身份证号码的验证
- C++与Java的区别
- Java使用jdbc连接数据库
- 七大常用PHP算法
- Bootstrap Datetimepicker 年月日时分秒控件
- ipconfig查询时默认网关会出现0.0.0.0问题的解决