一、简述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];
 
 

iOS------主题设置-->Appearance的更多相关文章

  1. IDEA 的主题设置

    1.主题设置(Appearance& Behavior) 补充1:设置编辑区的主题 (1)IDEA提供了两个编辑区的主题,如下所示 (2)如果想要更多的主题效果,可以到 http://www. ...

  2. iOS 怎么设置 UITabBarController 的第n个item为第一响应者?

    iOS 怎么设置 UITabBarController 的第n个item为第一响应者? UITabBarController 里面有个属性:selectedIndex @property(nonato ...

  3. iOS开发小技巧--iOS中设置applicationIconBadgeNumber遇到的问题

    iOS中设置applicationIconBadgeNumber 在iOS7中直接设置applicationIconBadgeNumber没有问题,但是在iOS8之后设置applicationIcon ...

  4. Android Studio IDE 主题设置

    1.界面主题设置,如下图: 2.代码字体设置,如下图:

  5. 【转】iOS中设置导航栏标题的字体颜色和大小

    原文网址:http://www.360doc.com/content/15/0417/11/20919452_463847404.shtml iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参 ...

  6. Webstorm6的汉化以及主题设置

    Webstorm6.0.2界面截图: webstorm作为一款前端开发软件,被业内称为神器,下面是下载地址. 需要的人太多,邮件不过来,传到这边方便大家下载 汉化包 http://www.jetbra ...

  7. iOS中设置导航栏标题的字体颜色和大小

    iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参考下. 在平时开发项目的时候,难免会遇到修改导航栏字体大小和颜色的需求,一般使用自定义视图的方法,其实还存在一种方法. 方法一:(自定义视图的 ...

  8. phpstorm9整合本地apache和豆沙绿主题设置(附资源)

    ♣phpstorm9下载(安装包和注册码) ♣phpstorm9自带apache和自定义apache服务器 ♣phpstorm9豆沙绿主题设置(附我的主题包) 说明:如果还未安装apache和php7 ...

  9. emacs24 颜色主题设置

    Emacs24 颜色主题设置 在Linux上写程序,永远绕不过的2个东西就是vi和emacs.emacs是早晚要接触的东西.本文就从配置颜色主题(color-theme)开始.用命令:$ sudo a ...

随机推荐

  1. 洛谷 P1345 [USACO5.4]奶牛的电信Telecowmunication

    题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...

  2. 在DataGridView控件中启用换行

    实现效果: 知识运用: DataGridView控件公共属性DefaultCellStyle的WrapMode属性 public DataGridViewTriState WrapMode {  ge ...

  3. BCB:AnsiString BSTR WideString

    WideString wstr;AnsiString astr;wchar_t *wp;//或者 BSTR wp; wp=wstr.c_bstr(); //WideString转化为BSTRwstr= ...

  4. c3p0,dbcp和proxool

    关于c3p0.dbcp和proxool,之类的比较,配置在网上有很多的文章,我这边就不浪费大家的时间了,主要讲下我用过这三个之后的体会. dbcp:框架以前使用的是dbcp,网上说,有很多BUG,至少 ...

  5. php面试相关

    22.描述一下大流量高并发量网站的解决方案 答: 1.确认服务器硬件是否足够支持当前的流量. 2.使用memcache缓存技术,将动态数据缓存到内存中,动态网页直接调用这些文件,而不必在访问数据库. ...

  6. 119. Pascal's Triangle II@python

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  7. An Intuitive Explanation of Convolutional Neural Networks

    https://ujjwalkarn.me/2016/08/11/intuitive-explanation-convnets/ An Intuitive Explanation of Convolu ...

  8. Python pip 使用国内镜像

    ## 推荐源```https://mirrors.aliyun.com/pypi/simple/ 阿里镜像,速度快.稳定https://pypi.douban.com/simple/ 豆瓣镜像```# ...

  9. Unity基础-Input接口

    input 底层的设备输入接口,在开发中很少用到 Input.GetKey() // Update is called once per frame void Update () { if (Inpu ...

  10. 配置基于Vim的Python开发环境

    配置基于Vim的Python开发环境插件 Vundle YouCompleteMe NERDTree Vim-Jinja2-Syntax set nocompatible " be iMpr ...