苹果通过两个协议(UIAppearance和UIAppearanceContainer)规范了对许多UIKit控件定制的支持。所有遵循UIAppearance协议的UI控件通过定制都可以呈现各种外观。不仅如此,UIAppearance协议甚至允许开发者基于控件所属的区域指定不同的外观。也就是说,当某个控件包含在特定视图中时,可以指定它的外观(如UIBarButtonItem的tintColor)。也可以获取该控件类的外观代理对象,用该代理定制外观来实现,
定制应用中所有UIBarButtonItem的颜色,可以在UIBarButtonItem的外观代理中设置tintColor:
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
原来setTintColor方法就在UIBarButtonItem中了,但它只会作用到某个特定的控件实例,而不是所有的此类控件。
根据包含于哪个视图而现实不同的样式:
[[UIBarButtonItem appearanceWhenContainedIn: [UINavigationBar class], nil]
setTintColor:[UIColor redColor]];
其中所有带有UI_APPEARANCE_SELECTOR标记的属性都支持通过外观代理来定制。举个例子,UINavigationBar.h中的tintColor属性带有UI_APPEARANCE_SELECTOR标记:
@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;