一、简述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. [机器视觉] SIFT特征-尺度不变特征理解

    SIFT特征-尺度不变特征理解 简介 SIFT,即尺度不变特征变换(Scale-invariant feature transform,SIFT),是用于图像处理领域的一种描述.这种描述具有尺度不变性 ...

  2. ansible 任务委派 delegate_to

    ansible 任务委派功能delegate_to run_noce: true  在一个主机上面只执行一次一个任务. ,如果没有这个参数的话,每个playbook中的组的主机都会执行一次. 我们有的 ...

  3. 【转】HTTP Live Streaming直播(iOS直播)技术分析与实现

    HTTP Live Streaming直播(iOS直播)技术分析与实现 不经意间发现,大半年没写博客了,自觉汗颜.实则2012后半年,家中的事一样接着一样发生,实在是没有时间.快过年了,总算忙里偷闲, ...

  4. Dojo常用函数

    1.array函数:和原生的JavaScript中的数组遍历方法forEach方法用法相同 define(['dojo/_base/declare', "dojo/_base/array&q ...

  5. c#自定义类型之间的转换(强制类型转换)

    public class ResultModel { public string PlateNumber { get; set; } public int PlateColor { get; set; ...

  6. Spring3中好用的工具类收集

    1) 请求工具类 org.springframework.web.bind.ServletRequestUtils //取请求参数的整数值: public static Integer getIntP ...

  7. nodejs写一个简单的Web服务器

    目录文件如 httpFile.js如下: const httpd = require("http"); const fs = require("fs"); // ...

  8. vc文件操作汇总—支持wince

    一.判断文件及文件夹是否存在 // 判断文件是否存在 BOOL IsFileExist(const CString& csFile) { DWORD dwAttrib = GetFileAtt ...

  9. input类型

    由于我们学习一个新的知识,而以前的都差不多忘完了,下面来复习我们以前学习的input的类型: button     点击的按钮                date    年/月/日 CheckBo ...

  10. 20181205(模块循环导入解决方案,json&pickle模块,time,date,random介绍)

    一.补充内容 循环导入 解决方案: 1.将导入的语句挪到后面. ​ 2.将导入语句放入函数,函数在定义阶段不运行 #m1.pyprint('正在导入m1')   #②能够正常打印from m2 imp ...