[UINavigationBar appearance]
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. return YES; }
[self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
#define UI_APPEARANCE_SELECTOR __attribute__((annotate("ui_appearance_selector")))
@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
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusItem, UIFocusItemContainer, CALayerDelegate>
UIImage *bg = [UIImage pureImageWithColor:[UIColor brownColor]];
[[UINavigationBar appearance] setBackgroundImage:bg forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTintColor:[UIColor blueColor]];
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]];
[[UINavigationBar appearanceForTraitCollection:[UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact]] setBarTintColor:[UIColor purpleColor]];
[[UIButton appearance] setBackgroundColor:[UIColor yellowColor]]; [[UIButton appearance] setTitle:@"同一設置" forState:UIControlStateNormal];
[[UIButton appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setTitle:@"比較特別" forState:UIControlStateNormal]; [[UIButton appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setBackgroundColor:[UIColor redColor]];