美文网首页iOS开发技巧iOS进阶指南iOS技术点
使用appearance统一设置整个应用中的 UITabBarI

使用appearance统一设置整个应用中的 UITabBarI

作者: 冷洪林 | 来源:发表于2016-10-27 10:31 被阅读954次
在开发中经常遇到要多次重复需求,此时用appearance可以大大简化工作量,但是appearance使用的前提是,该类已经遵守@protocol UIAppearance <NSObject>协议,并且实现+ (instancetype)appearance方法.
// 设置整个应用中的 UITabBarItem 按钮的颜色
UITabBarItem *item = [UITabBarItem appearance];
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor blackColor];
[item setTitleTextAttributes:attrs forState:UIControlStateSelected];


// 设置字体的尺寸:只有正常状态下设置才有效果
NSMutableDictionary *attrsNor = [NSMutableDictionary dictionary];
attrsNor[NSFontAttributeName] = [UIFont systemFontOfSize:13];
[item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];
appearance和appearanceWhenContainedIn的区别:appearance使用要慎重,因为appearance是获取的整个应用中的,很可能一不小心就把其他地方的也改了,使用appearanceWhenContainedIn就会相对较安全,它只会统一修改制定类里面的属性
// 设置某各类中的 UITabBarItem 按钮的颜色
UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor blackColor];
[item setTitleTextAttributes:attrs forState:UIControlStateSelected];
appearance使用注意:
  • 一定要在控件显示之前设置才有用,一般会放在+ (void)load方法中而不放在+(void)initialize中,因为+ (void)load方法只会调用一次,+(void)initialize可能会调用多次,使用时还需要判断:
    + (void)initialize
    {
    if (self == [LHLTabBarController class]) {
    }
    }
  • 只有被UI_APPEARANCE_SELECTOR宏修饰的属性,才能使用appearance统一设置
    - (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

相关文章

  • 使用appearance统一设置整个应用中的 UITabBarI

    在开发中经常遇到要多次重复需求,此时用appearance可以大大简化工作量,但是appearance使用的前提是...

  • appearance

    通过appearance统一设置所有UITabBarItem的文字属性NSMutableDictionary *a...

  • UITabBarController相关知识

    UITabBarItem的相关属性 // 通过appearance统一设置所有UITabBarItem的文字属性/...

  • UI_APPEARANCE_SELECTOR

    iOS中, 在属性的后边出现UI_APPEARANCE_SELECTOR 含义: 表示该属性可以通过统一一次设置,...

  • ios15.0适配问题,UITableView显示错乱

    修改属性prefetchingEnabled,因控件都是遵循appearance,进行全局统一设置 if(@a...

  • 01-appearance的使用

    总结: appearance的使用使得tabBarItem的文字格式只设置一次, 不用在每个子控制器中重复设置, ...

  • 关于ios appearance

    + (id)appearance这个方法是统一全部改,比如你设置UINavBar的tintColor,你可以这样写...

  • iOS-appearance

    原文 (id)appearance这个方法是统一全部改,比如你设置UINavBar的tintColor,你可以这样...

  • appearance使用

    使用appearance可以获取整个应用程序下的某个类。例如想修改TabBar按钮文字的默认选中颜色,就如图,修改...

  • idea修改编辑器字体大小

    设置 -> Appearance & Behavior -> Appearance -> Use custom ...

网友评论

本文标题:使用appearance统一设置整个应用中的 UITabBarI

本文链接:https://www.haomeiwen.com/subject/rpijuttx.html