在看一些第三方框架的時候發(fā)現(xiàn)了一些使用appearance方法的,于是就研究了一下,覺得還是挺有趣的:
1.關于Appearance:
@protocol UIAppearance <NSObject>
UIAppearance本身是一個協(xié)議,在協(xié)議中有以下幾種方法:
// 初始化方法,支持遵守UIAppearance協(xié)議的類,可以直接初始化出對應類,再進行屬性修改
+ (instancetype)appearance;
// 在指定類中修改屬性,后面跟class,(最后跟一個nil),這行代碼以后在指定類中創(chuàng)建的對象就會遵守進行的屬性修改,iOS9被下一個方法替換
+ (instancetype)appearanceWhenContainedIn:(nullable Class <UIAppearanceContainer>)ContainerClass, ... NS_REQUIRES_NIL_TERMINATION NS_DEPRECATED_IOS(5_0, 9_0, "Use +appearanceWhenContainedInInstancesOfClasses: instead") __TVOS_PROHIBITED;
// iOS9之后替換上一個方法的,我感覺就是將之前的class,nil變?yōu)橐粋€@[class]的數(shù)組
+ (instancetype)appearanceWhenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes NS_AVAILABLE_IOS(9_0);
// 在指定狀態(tài)下修改指定對象屬性,比如將在橫屏狀態(tài)和豎屏狀態(tài)下顯示不同顏色
+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait NS_AVAILABLE_IOS(8_0);
+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait whenContainedIn:(nullable Class <UIAppearanceContainer>)ContainerClass, ... NS_REQUIRES_NIL_TERMINATION NS_DEPRECATED_IOS(8_0, 9_0, "Use+appearanceForTraitCollection:whenContainedInInstancesOfClasses: instead") __TVOS_PROHIBITED;
+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait whenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes NS_AVAILABLE_IOS(9_0);
2. 支持UIAppearance類
** 1.UIActivitiIndicatorView**
** 2.UIBarButtonItem**
** 3.UIBarItem**
** 4.UINavgationBar**
** 5.UIPopoverControll**
** 6.UIProgressView**
** 7.UISearchBar**
** 8.UISegmentControll **
** 9.UISlider**
** 10.UISwitch**
** 11.UITabBar**
** 12.UITabBarItem**
** 13.UIToolBar**
** 14.UIView**
** 15.UIViewController**
3. 例子
- (void)viewDidLoad {
[super viewDidLoad];
blueView *bView = [[blueView alloc] init];
redView *rView = [[redView alloc] init];
[self.view addSubview:bView];
[self.view addSubview:rView];
// btn1在設置UIAppearance前添加到bView
UIButton *btn1 = [[UIButton alloc] init];
btn1.frame = CGRectMake(100, 100, 50, 50);
btn1.backgroundColor = [UIColor blackColor];
[bView addSubview:btn1];
// btn2在設置UIAppearance前添加到rView
UIButton *btn2 = [[UIButton alloc] init];
btn2.frame = CGRectMake(100, 100, 50, 50);
btn2.backgroundColor = [UIColor blackColor];
[rView addSubview:btn2];
// btn3在設置UIAppearance后添加到bView
UIButton *btn3 = [[UIButton alloc] init];
btn3.frame = CGRectMake(100, 200, 50, 50);
btn3.backgroundColor = [UIColor blackColor];
// btn4在設置UIAppearance后添加到rView
UIButton *btn4 = [[UIButton alloc] init];
btn4.frame = CGRectMake(100, 200, 50, 50);
btn4.backgroundColor = [UIColor blackColor];
// btn5在設置UIAppearance后添加到rView
UIButton *btn5 = [[UIButton alloc] init];
btn5.frame = CGRectMake(100, 300, 50, 50);
btn5.backgroundColor = [UIColor blackColor];
[rView addSubview:btn2];
//設置UIAppearance
[[UIButton appearanceWhenContainedInInstancesOfClasses:@[[redView class]]] setBackgroundColor:[UIColor greenColor]];
// 設置UIAppearance后修改btn4的顏色
btn4.backgroundColor = [UIColor blackColor];
[bView addSubview:btn3];
[rView addSubview:btn4];
[rView addSubview:btn5];
}
Paste_Image.png
如圖:
1. 由btn1與btn2對比可知:指定了redView類型的Appearance對redView上的btn產(chǎn)生影響;
2. 由btn1與btn3對比可知:指定了redView類型的Appearance并未對
3. blueView上的btn產(chǎn)生影響;
4. 由btn2與btn4對比可知:在指定Appearance之前修改屬性不會生效,在指定Appearance之后修改對象屬性,會生效;
5. 由btn2與btn5對比可知:指定Appearance前后添加到View上不會影響Appearance的效果;
4. 使用場景:
- 一次性指定默寫控件的特性比如自定義tabBarVC的UIBarButtonItem和UIBarItem等;
- 一件換膚:根據(jù)拼接的圖片或者文件名,批量刷新UI的圖片等(通過拼接將red_backImage變成blue_backImage);
具體的代碼有時間寫完再更新謝謝
5. 參考博客,十分感謝作者:
- iOS UIAppearance使用詳解:
http://blog.sina.com.cn/s/blog_9693f61a0101f1rs.html - iOS --思考appearance主題:
http://www.reibang.com/p/8262ec74bfc7 - iOS --統(tǒng)一UINavigationBar的主題appearance:
http://www.reibang.com/p/73bfbc7210a7 - WWDC 2014 Session筆記 - iOS界面開發(fā)的大一統(tǒng)
https://onevcat.com/2014/07/ios-ui-unique/ - iOS --統(tǒng)一UINavigationBar的主題appearance