UIAppearance
是一个协议
@protocol UIAppearance <NSObject>
UIView
默认已经遵守了这个协议
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusItem, CALayerDelegate>
来看看UIAppearance都有什么方法
+ (instancetype)appearance;
+ (instancetype)appearanceWhenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes NS_AVAILABLE_IOS(9_0);
+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait NS_AVAILABLE_IOS(8_0);
+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait whenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes NS_AVAILABLE_IOS(9_0);
让某一类控件同时表现某种属性
//导航条上标题的颜色
NSDictionary *navbarTitleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
//TabBar选中图标的颜色,默认是蓝色
[[UITabBar appearance] setTintColor:[UIColor greenColor]];
//导航条的背景颜色
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
//TabBar的背景颜色
[[UITabBar appearance] setBarTintColor:[UIColor brownColor]];
[UISearchBar appearance].tintColor = [UIColor redColor];
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor magentaColor];
pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
[[UITextField appearance] setTintColor:[UIColor purpleColor]];
[[UITextView appearance] setTintColor:[UIColor darkGrayColor]];
让某一类控件在另一种控件中同时变现某种属性
//当某个class被包含在另外一个class内时,才修改外观。
[[UIButton appearanceWhenContainedInInstancesOfClasses:@[[UIView class]]] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[[UITextView appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTextColor:[UIColor yellowColor]];
如图所示
Simulator Screen Shot - iPhone 6 - 2019-06-17 at 15.20.21.png
网友评论