iOS 使用UI控件的外觀協議UIAppearance進行設置默認UI控件樣式

在iOS開發中,常常會對UINavigationBar的樣式進行全局樣式。採用的設置方式有兩種:
第一種,採用方式以下:
[UINavigationBar appearance]
這種是對一類對象的默認全局外觀樣式設置,它對設置時機有要求。
一般須要在UIWindow的viewlayout以前。錯過了時機後,設置是沒有效果的。
能夠選擇在下面方法內設置:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    return YES;
}
第二種,採用方式以下:
[self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
這種是即時性的,設置後就會有效果。
 
第一種方式經過appearance使得UINavigationBar產生了全局默認樣式,那麼appearance是什麼東西呢?
Appearance(外觀協議)講解
UIAppearance文件包含於UIKit框架中,該文件很短,包括註釋加起來總共56行
點擊去看看。
裏面有一個宏定義和兩個協議聲明,分別以下:
1.宏定義:
#define UI_APPEARANCE_SELECTOR __attribute__((annotate("ui_appearance_selector")))
由註釋可知:凡是被這個宏標記的屬性方法,均可以當用做全局樣式的調用方法。
如:UIView的backgroundColor屬性
@property(nullable, nonatomic,copy)            UIColor          *backgroundColor UI_APPEARANCE_SELECTOR; //

2.外觀容器協議:app

@protocol UIAppearanceContainer <NSObject> @end

這是個空協議,裏面啥都沒有框架

3.外觀協議:ide

@protocol UIAppearance <NSObject>
//返回實現了此外觀協議的UI控件實例,用這個返回的對象設置的屬性是全局性。
+ (instancetype)appearance;
//返回實現了此外觀協議的UI控件實例,用這個返回的對象設置的屬性只對ContainerClass內部包裹的對象有效。
+ (instancetype)appearanceWhenContainedIn:(nullable Class <UIAppearanceContainer>)ContainerClass, ... NS_REQUIRES_NIL_TERMINATION NS_DEPRECATED_IOS(5_0, 9_0, "Use +appearanceWhenContainedInInstancesOfClasses: instead") __TVOS_PROHIBITED;
//是iOS9以後的替代方法,做用和上面的同樣。
+ (instancetype)appearanceWhenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes NS_AVAILABLE_IOS(9_0);
//是iOS8新增的方法,是對不一樣的佈局方案(緊湊型,普通型)採用不一樣的外觀樣式
+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait NS_AVAILABLE_IOS(8_0);
//與上面的方法類似,多了一個參數條件,只對ContainerClass內部包裹的對象有效。
+ (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;
//iOS9後的新方法,是對上面方法的替換。
+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait whenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes  NS_AVAILABLE_IOS(9_0);
@end
在上面代碼中對裏面定義的5個方法進行了說明。
 
遵照了這兩個協議的類,能夠進行同一默認外觀設置,那麼哪些類遵照了這兩個協議了呢?
看一下UI控件的父類UIView
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusItem, UIFocusItemContainer, CALayerDelegate>
經過上面UIView的定義能夠發現,UIView遵照了外觀協議和外觀容器協議。
也就是說全部的UI控件,只有其屬性方法被UI_APPEARANCE_SELECTOR宏標示了,那麼就能夠進行全局外觀
默認設置。
 
這裏列一下UINavigationBar一般用的的全局設置。
//⚠️: 1與2互斥,且1的優先級高
//1.設置導航背景
UIImage *bg = [UIImage pureImageWithColor:[UIColor brownColor]];
[[UINavigationBar appearance] setBackgroundImage:bg forBarMetrics:UIBarMetricsDefault];
//2.設置導航顏色
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
//3.設置導航文字顏色
[[UINavigationBar appearance] setTintColor:[UIColor blueColor]];
//4.設置導航字體
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName, shadow, NSShadowAttributeName, [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
//5.特定環境或某種場合下外觀樣式設置(水平緊湊型,UINavigationBar默認外觀爲粉色。)
[[UINavigationBar appearanceForTraitCollection:[UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact]] setBarTintColor:[UIColor purpleColor]];

 

另外對UIButton的全局默認設置以下:
//讓一類控件同時表現出某種屬性
[[UIButton appearance] setBackgroundColor:[UIColor yellowColor]];
[[UIButton appearance] setTitle:@"同一設置" forState:UIControlStateNormal];
//讓一類控件在某種環境下表現出某種外觀樣式
[[UIButton appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setTitle:@"比較特別" forState:UIControlStateNormal];
[[UIButton appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setBackgroundColor:[UIColor redColor]];
UI控件的默認全局設置方式符合預期。
那麼iOS開發中,全部的UI控件均可以經過上面兩種方法進行全局樣式默認設置和自定義設置了。
相關文章
相關標籤/搜索