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.外觀容器協議:

@protocol UIAppearanceContainer <NSObject> @end

這是個空協議,里面啥都沒有

3.外觀協議:

@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控件都可以通過上面兩種方法進行全局樣式默認設置和自定義設置了。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM