Xummer

UIAppearance的使用

UIAppearance

iOS 5以后加了UIAppearance来控制UI的全局的显示+ appearance 或某一个类中的显示+ appearanceWhenContainedIn:.

UIAppearance只对有UI_APPEARANCE_SELECTOR 标示的property有效,如UINavigationBar.h => tintColor

@property(nonatomic,retain) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; 

Apple提供的方法

+ appearance
+ appearanceWhenContainedIn:

用法

[UINavigationBar appearance].barTintColor = [UIColor redColor];

没有 UI_APPEARANCE_SELECTOR 的解决方法

创建Category, 并添加带有UI_APPEARANCE_SELECTOR 标示的property,虽然Category不能通过正常(这里不说runtime)加变量,但是这边只是用了setter方法,并没有实际用到这个property。

// ====== .h ======

#import <UIKit/UIKit.h>

@interface UILabel (Appearance)

@property (assign, nonatomic) UIColor *labelBackgroundColor UI_APPEARANCE_SELECTOR;

@end

// ====== .m ======

#import "UILabel+Appearance.h"

@implementation UILabel (Appearance)
@dynamic labelBackgroundColor;

- (void)setLabelBackgroundColor:(UIColor *)labelBackgroundColor {
    [super setBackgroundColor:labelBackgroundColor];
}

@end
[[UILabel appearance] setLabelBackgroundColor:[UIColor clearColor]];

###参考资料

-以上-