Appearance

UIAppearance是一个协议

@protocol UIAppearance <NSObject>

UIView默认已经遵守了这个协议

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusItem, CALayerDelegate>

来看看UIAppearance都有什么方法

+ (instancetype)appearance;

+ (instancetype)appearanceWhenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes NS_AVAILABLE_IOS(9_0);

+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait NS_AVAILABLE_IOS(8_0);

+ (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait whenContainedInInstancesOfClasses:(NSArray<Class <UIAppearanceContainer>> *)containerTypes  NS_AVAILABLE_IOS(9_0);

让某一类控件同时表现某种属性

    //导航条上标题的颜色
    NSDictionary *navbarTitleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    [[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
    [[UINavigationBar appearance] setTintColor:[UIColor redColor]];
    
    //TabBar选中图标的颜色,默认是蓝色
    [[UITabBar appearance] setTintColor:[UIColor greenColor]];
    
    //导航条的背景颜色
    [[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
    
    //TabBar的背景颜色
    [[UITabBar appearance] setBarTintColor:[UIColor brownColor]];
    
    [UISearchBar appearance].tintColor = [UIColor redColor];

    UIPageControl *pageControl = [UIPageControl appearance];
    pageControl.pageIndicatorTintColor = [UIColor magentaColor];
    pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
    
    [[UITextField appearance] setTintColor:[UIColor purpleColor]];
    [[UITextView appearance]  setTintColor:[UIColor darkGrayColor]];

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

    //当某个class被包含在另外一个class内时,才修改外观。
    [[UIButton appearanceWhenContainedInInstancesOfClasses:@[[UIView class]]] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
   
     [[UITextView appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTextColor:[UIColor yellowColor]];

如图所示


Simulator Screen Shot - iPhone 6 - 2019-06-17 at 15.20.21.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,887评论 1 32
  • Appearance Proxies UIAppearance是一个协议 UIView默认已经遵守了这个协议 来看...
    ttys苹果核阅读 11,065评论 0 18
  • 一、前言 我们经常设置UINavigationBar、UITabBar等主题,我们有没有想过,为什么我们可以给它们...
    iOS_成才录阅读 10,417评论 2 20
  • 在iOS 5之后,出现了两个Protocol,分别是UIAppearance 以及UIAppearanceCont...
    ac41d8480d04阅读 10,432评论 0 8
  • 我多想当一名医生可以当一名救死扶伤的医生,为他们减轻痛苦医好他们的身体实他们的身体能够健康、快乐的和家人们生...
    王明盛阅读 12,369评论 0 0

友情链接更多精彩内容