一、简述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. Windows Experience Index

    The Windows Experience is still there--even in build 9860.  However, the GUI was retired with Window ...

  2. Unity中实现全局管理类的几种方式

    (搬运自我在SegmentFault的博客) 如何在Unity中实现全局管理类?由于Unity脚本的运行机制和面向组件编程(COP)的思想,实现起来和普通的方式略有差别. 第一种方式是使用静态类.适合 ...

  3. Firefox火狐广告过滤插件Adblock Plus过滤规则包[中文维护小组]

    如果你经常使用Firefox火狐浏览器那么一定知道Adblock Plus这款广告过滤插件,功能非常强大,但是Adblock Plus广告过滤插件自带的过滤规则并不多,而且也不太适合我们中国的网站,在 ...

  4. UI与数据分离 与 UI的演进

    解藕的好处:UI内部模块能够灵活的变化. MVC或者三层架构着重强调了数据.业务逻辑和UI的分离. (MVC中的C只是UI和业务逻辑模块间的一个中转组件,理论上应该是个轻模块.) 以前的关注的解藕技术 ...

  5. PAT (Basic Level) Practise (中文)- 1015. 德才论 (25)

    http://www.patest.cn/contests/pat-b-practise/1015 宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡 ...

  6. MVCPager学习小记

    1.PageIndexParameterName怎么关联? 答:其实就是Action里面的pageindex参数 例子: @Html.Pager(Model, new PagerOptions { P ...

  7. Boo who-freecodecamp算法题目

    Boo who 1.要求 检查一个值是否是基本布尔类型,并返回 true 或 false. 基本布尔类型即 true 和 false 2.思路 利用switch语句判断输入的数据是true/false ...

  8. 【细节题 离线 树状数组】luoguP4919 Marisa采蘑菇

    歧义差评:但是和题意理解一样了之后细节依然处理了很久,说明还是水平不够…… 题目描述 Marisa来到了森林之中,看到了一排nn个五颜六色的蘑菇,编号从1-n1−n,这些蘑菇的颜色分别为col[1], ...

  9. centos6启动故障排除

    centos6中boot文件被全部删除的故障排除 /boot文件里关于启动的核心文件有三个,/vmlinuz-2.6.32-696.e16.x86_64,initramfs-2.6.32-696.el ...

  10. 消息队列之JMS和AMQP对比

    https://blog.csdn.net/hpttlook/article/details/23391967 AMQP & JMS对比 初次接触消息队列时,在网上搜索,总是会提到如JMS.A ...