iOS後屬性帶UI_APPEARANCE_SELECTOR 能夠統一設置全局做用html
例如:ios
1>開關控件git
@property(nullable, nonatomic, strong) UIColor *onTintColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; 程序員
能夠統一設置開關的onTintColor樣式.不過開關控件的顏色屬性比較特殊github
1.只在添加時做用一次.app
2.添加結束後.ide
3.之後設置便再也不改變.ui
4.也能夠移出開關的父控件.而把開關重新加入到window上.atom
在iOS屬性後有UI_APPEARANCE_SELECTOR標誌均可以一次性統一設置.這種狀況還有不少.好比說統一設置UITabbarItem的文字顏色 方法以下:spa
- (void)setTitleTextAttributes:(nullable NSDictionary*)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
appearance是apple在iOS5.0上加的一個協議,它讓程序員能夠很輕鬆地改變某控件的全局樣式(背景)
支持UIAppearance協議的類能夠訪問appearance selector ,它爲receiver返回appearance proxy,我麼能夠給proxy發一些消息,諸如setTintColor:等
可是它並非支持全部的UI類。下面列出它支持的類
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
例如:
[[UINavigationBarappearance] setTintColor:[UIColorblackColor]];
[[UISearchBarappearance] setTintColor:[UIColorblackColor]];
注意:
初學者確定會任意調用方法,大部分方法時無效的,若是調用時會拋出unknown selector 異常
那麼如何查看你調用的方法時有效的呢,咱們能夠在此類的頭文件中查看包含「UI_APPEARANCE_SELECTOR」常量的方法。
例如UIToolBar
它支持下列方法
@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;
- (void)setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIToolbarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (UIImage *)backgroundImageForToolbarPosition:(UIToolbarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (void)setShadowImage:(UIImage *)shadowImage forToolbarPosition:(UIToolbarPosition)topOrBottom NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
- (UIImage *)shadowImageForToolbarPosition:(UIToolbarPosition)topOrBottom NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
瞭解更多請訪問:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html。
補上實例代碼:
+ (void) initialize
{
if (self == [DACircularProgressView class]) {
DACircularProgressView *circularProgressViewAppearance = [DACircularProgressView appearance];
[circularProgressViewAppearance setTrackTintColor:[[UIColor whiteColor] colorWithAlphaComponent:0.3f]];
[circularProgressViewAppearance setProgressTintColor:[UIColor whiteColor]];
[circularProgressViewAppearance setInnerTintColor:nil];
[circularProgressViewAppearance setBackgroundColor:[UIColor clearColor]];
[circularProgressViewAppearance setThicknessRatio:0.3f];
[circularProgressViewAppearance setRoundedCorners:NO];
[circularProgressViewAppearance setClockwiseProgress:YES];
[circularProgressViewAppearance setIndeterminateDuration:2.0f];
[circularProgressViewAppearance setIndeterminate:NO];
}
}
更多:
1.http://blog.csdn.net/lxl_815520/article/details/51360878
2.https://southpeak.github.io/2015/07/20/cocoa-uikit-uiapearance/
3.https://www.cnblogs.com/Rinpe/p/5351639.html
4.https://www.cnblogs.com/salam/archive/2013/01/30/appearance.html