iOS ------ Theme Settings -> Appearance

A brief UIAppearance  what is?

  1.UIAppearance is a protocol

  @protocol UIAppearance <NSObject>

  只要遵守了UIAppearance协议的类,都可以设置主题:

But it is not supported by all UI classes. It supports the following list of classes
  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

Note : set the properties of an object by topic premise:  属性后面是否带有UI_APPEARANCE_SELECTORthe method

例如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;
  • Another example: You can set the text UITabBarItem theme
- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  • Case: Set the text color in all UITabBarItem, general and selected
.UIAppearance都有什么方法
+ (instancetype)appearance;
+ (instancetype)appearanceWhenContainedIn:(nullable Class <UIAppearanceContainer>)ContainerClass, ... NS_REQUIRES_NIL_TERMINATION NS_DEPRECATED_IOS(5_0, 9_0, "Use +appearanceWhenContainedInInstancesOfClasses: instead") __TVOS_PROHIBITED;
+ (instancetype)appearanceWhenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes NS_AVAILABLE_IOS(9_0);
+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait NS_AVAILABLE_IOS(8_0);
+ (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;
+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait whenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes NS_AVAILABLE_IOS(9_0);

 

 

1. Have a certain type of control while exhibiting a property

[[UIButton appearance] setBackgroundColor:[UIColor blackColor]];
[[UIButton appearance] setTitle:@"呵呵哒" forState:UIControlStateNormal];

2. Have a certain type of control is realized while a property in another control

[[UIButton appearanceWhenContainedInInstancesOfClasses:@[[UIView class]]] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

Above this sentence means that ---- the titleColor UIButton of UIView above are grayed
effect, then you can try yourself.

Third, the use of themes appearance, whether it will enter into force, when it will take effect

1 主题会生效: setting Control topic, after adding controls,
  • When you add the controls, checks theme that moment added, based on the theme set the controls = "theme will take effect
2 主题不会生效: first add controls, after setting the theme
  • Controls have been added, after setting the theme for the controls previously added does not work
  • If, before adding controls, setting the theme, the theme of failure, how do we solve it?
3, how to solve the theme failures
    • Scenario 1: The View host controller control is removed from the window, because the theme has been determined, View and then the control is added to the window controller = "At this time the theme will play a role.


       
      • Disadvantages: poor scalability, because the controls on this theme can not solve all controllers
    • Option 2: Get an array of applications to the windows, and then traverse the delete view, then you can add on
      • note:控件是否销毁,要看是否有强指针指向它

          

IV Summary:
1, controls compliance UIAppearance agreement to set up the control as appearance
2, only to be UI_APPEARANCE_SELECTOR this macro modified to use the appearance attribute set, this function has no other properties
3, appearance is set to be displayed in the control before setting, otherwise may be invalid

It provides the following two methods:

+ (id)appearance;

This method is unified all changed, for example you set tintColor UINavigationBar, you can write:

[[UINavigationBar appearance] setTintColor:myColor];
+ (id)appearanceWhenContainedIn:(Class <>)ContainerClass,...

This method may be arranged to change a class, for example:
setting effects in UIBarButtonItem UINavigationBar, UIPopoverController, UITabbar in, it can be written:

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIPopoverController class],[UITabbar class],nil] setTintColor:myColor];

Note: Use the best use of UI appearance set global settings, start setting before all the initialization interface, or it may fail.

DETAILED UI look modified as follows:

BACKGROUND 1. Modify the navigation bar
code is as follows:

UINavigationBar * appearance = [UINavigationBar appearance];
UIImage *navBgImg =[UIImage imageNamed:@"bgImage"]; [appearance setBackgroundImage:navBgImg forBarMetrics: UIBarMetricsDefault];

2. tab bar (UITabBar)
code is as follows:

UITabBar *appearance = [UITabBar appearance];
//设置背景图片 [appearance setBackgroundImage:[UIImage imageNamed:@"bgImage"]]; //设置选择item的背景图片 UIImage * selectedImage =[[UIImage imageNamed:@"sliderImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2)] ; [appearance setSelectionIndicatorImage:selectedImage];

3. A segmented control (UISegmentControl)
code is as follows:

UISegmentedControl *appearance = [UISegmentedControl appearance];

//Segmenteg正常背景 [appearance setBackgroundImage:[UIImage imageNamed:@"image"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; //Segmente选中背景 [appearance setBackgroundImage:[UIImage imageNamed:@"bgImage"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; /* Segmente左右都未选中时的分割线 BarMetrics表示navigation bar的状态,UIBarMetricsDefault 表示 portrait状态(44pixel height),UIBarMetricsLandscapePhone 表示landscape状态(32pixel height */ [appearance setDividerImage:[UIImage imageNamed:@"line"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [appearance setDividerImage:[UIImage imageNamed:@"line"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [appearance setDividerImage:[UIImage imageNamed:@"line"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; //字体 NSDictionary *textAtt1 = @{ NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor blueColor], NSShadowAttributeName: [UIColor whiteColor]}; [appearance setTitleTextAttributes:textAtt1 forState:1]; NSDictionary *textAtt2 = @{ NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor blueColor], NSShadowAttributeName: [UIColor whiteColor]}; [appearance setTitleTextAttributes:textAtt2 forState:0];

4.UIBarbutton
Note: UIBarbutton there leftBarButton, rightBarButton and backBarButton, wherein backBarButton with arrows due needs to be set individually. barButton background setting is ios6.0 and beyond, and backbutton is ios5.0 and beyond, here to pay attention!

code show as below:

//修改导航条上的UIBarButtonItem
UIBarButtonItem *appearance = [UIBarButtonItem appearanceWhenContainedIn: [UINavigationBar class], nil]; //设置导航栏的字体包括backBarButton和leftBarButton,rightBarButton的字体 NSDictionary *textAttributes = @{ NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor blueColor], NSShadowAttributeName: [UIColor whiteColor]}; [appearance setTitleTextAttributes:textAttributes forState:1]; //forState为0时为下正常状态,为1时为点击状态。 //修改leftBarButton,rightBarButton背景效果 [appearance setBackgroundImage:[UIImage imageNamed:@"image"] forState:UIControlStateNormal style:UIBarButtonItemStyleBordered barMetrics:UIBarMetricsDefault]; [appearance setBackgroundImage:[UIImage imageNamed:@"image"] forState:UIControlStateHighlighted style:UIBarButtonItemStyleBordered barMetrics:UIBarMetricsDefault]; //backBarButton需要单独设置背景效果,只能在ios6.0以后才能用 [appearance setBackButtonBackgroundImage:[UIImage imageNamed:@"bgImage"] forState:0 barMetrics:UIBarMetricsDefault]; [appearance setBackButtonBackgroundImage:[UIImage imageNamed:@"image"] forState:1 barMetrics:UIBarMetricsDefault]; [appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(2, -1) forBarMetrics:UIBarMetricsDefault];

5. Toolbar (UIToolbar)

UIToolbar *appearance = [UIToolbar appearance];
//样式和背景二选一即可,看需求了 //样式(黑色半透明,不透明等)设置 [appearance setBarStyle:UIBarStyleBlackTranslucent]; //背景设置 [appearance setBackgroundImage:[UIImage imageNamed:@"bgImage"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

Guess you like

Origin www.cnblogs.com/lijinfu-software/p/11362897.html