iOS------主题设置-->Appearance

一、简述UIAppearance 是什么?

  1.UIAppearance是一个协议

  @protocol UIAppearance <NSObject>

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

但是它并不是支持所有的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

注意通过主题对象设置属性的前提: 属性后面是否带有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;
  • 再例如:可以设置UITabBarItem文字主题
- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state
NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  • 案例:设置所有的UITabBarItem,普通与选中状态下的文字颜色
二.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.让某一类控件同时表现某种属性

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

2.让某一类控件在另一种控件中同时变现某种属性

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

上面这句话的意思 就是----使UIView上面的UIButton的titleColor都变成灰色的
效果的话大家可以自己试试。

三、运用主题appearance,是否会生效,何时会生效

1、主题会生效:先设置控件主题,后添加控件,
  • 添加控件时,添加的那一刻会检查主题,会根据主题设置控件 =》主题会生效
2、主题不会生效:先添加控件,后设置主题
  • 控件已经添加,后设置主题,对以前的添加的控件不起作用了
  • 如果,先添加控件,后设置主题,主题失效,我们该如何解决呢?
3、如何解决主题失效问题
    • 方案1 :将控件所在控制器的View从窗口移除,因为主题已经确定了,再将控件所在控制器的View添加到窗口上 =》 此时主题就起作用了。


       
      • 缺点:扩展性不好,因为这样不能解决所有控制器上的控件的主题
    • 方案2:获取到应用的windows数组,然后遍历,将view删除,然后添加上即可
      • 注意:控件是否销毁,要看是否有强指针指向它

          

四、总结:
1、控件遵守了UIAppearance协议,才能对控件进行appearance设置
2、只有被UI_APPEARANCE_SELECTOR这个宏修饰的属性才能使用appearance进行设置,其他属性则不具备该功能
3、appearance设置需要在该控制显示之前设置完成,否则可能无效

它提供如下两个方法:

+ (id)appearance;

这个方法是统一全部改,比如你设置UINavigationBar的tintColor,你可以这样写:

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

这个方法可设置某个类的改变,例如:
设置UIBarButtonItem 在UINavigationBar、UIPopoverController、UITabbar中的效果,就可以这样写:

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

请注意:使用appearance设置UI效果最好采用全局的设置,在所有界面初始化前开始设置,否则可能失效。

具体UI外观修改如下:

1.修改导航栏背景
代码如下:

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

[appearance setBackgroundImage:navBgImg forBarMetrics:
  UIBarMetricsDefault];

2.标签栏(UITabbar)
代码如下:

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.分段控件(UISegmentControl)
代码如下:

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
注意:UIBarbutton有leftBarButton,rightBarButton和backBarButton,其中backBarButton由于带有箭头,需要单独设置。barButton背景设置是ios6.0及以后的,而backbutton是ios5.0及以后的,这里要注意!

代码如下:

//修改导航条上的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.工具栏(UIToolbar)

UIToolbar *appearance = [UIToolbar appearance];
//样式和背景二选一即可,看需求了
//样式(黑色半透明,不透明等)设置
[appearance setBarStyle:UIBarStyleBlackTranslucent];
//背景设置
[appearance setBackgroundImage:[UIImage imageNamed:@"bgImage"]
            forToolbarPosition:UIToolbarPositionAny
                    barMetrics:UIBarMetricsDefault];
 
 
posted @ 2017-03-10 10:39  Van·Zhang  阅读(779)  评论(0编辑  收藏  举报