UIAppearance
Use the UIAppearance
protocol to get the appearance proxy for a class. You can customize the appearance of instances of a class by sending appearance modification messages to the class’s appearance proxy.
Note: iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.
There are two ways to customize appearance for objects: for all instances, and for instances contained within an instance of a container class.
-
To customize the appearance of all instances of a class, use
appearance
to get the appearance proxy for the class. For example, to modify the bar background tint color for all instances ofUINavigationBar
:[[UINavigationBar appearance] setBarTintColor:myNavBarBackgroundColor];
-
To customize the appearances for instances of a class when contained within an instance of a container class, or instances in a hierarchy, use
appearanceWhenContainedIn:
to get the appearance proxy for the class. For example, to modify the appearance of bar buttons, based on the object that contains the navigation bar:[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setBackgroundImage:myNavBarButtonBackgroundImage forState:state barMetrics:metrics];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIPopoverController class], nil]
setBackgroundImage:myPopoverNavBarButtonBackgroundImage forState:state barMetrics:metrics];
[[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil]
setBackgroundImage:myToolbarButtonBackgroundImage forState:state barMetrics:metrics];
[[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], [UIPopoverController class], nil]
setBackgroundImage:myPopoverToolbarButtonBackgroundImage forState:state barMetrics:metrics];
In any given view hierarchy, the outermost appearance proxy wins. Specificity (depth of the chain) is the tie-breaker. In other words, the containment statement in appearanceWhenContainedIn:
is treated as a partial ordering. Given a concrete ordering (actual subview hierarchy), UIKit selects the partial ordering that is the first unique match when reading the actual hierarchy from the window down.
To support appearance customization, a class must conform to the UIAppearanceContainer
protocol and relevant accessor methods must be marked with UI_APPEARANCE_SELECTOR
.
==>
UIAppearance协议可以得到一个类的外观代理,。你能够自定义自定义一个对象的外观 通过发送一个需要改变的信息给一个类的外观代理。
。。。
UIAppearanceContainer
A class must adopt the UIAppearance
protocol to allow appearance customization using the UIAppearance
API.
To participate in the appearance proxy API, tag appearance property accessor methods in your header with UI_APPEARANCE_SELECTOR
.
Appearance property accessor methods must be of the form:
- (PropertyType)propertyForAxis1:(IntegerType)axis1 axis2:(IntegerType)axis2 axisN:(IntegerType)axisN; |
- (void)setProperty:(PropertyType)property forAxis1:(IntegerType)axis1 axis2:(IntegerType)axis2 axisN:(IntegerType)axisN; |
You may have no axes or as many as you like for any property.
The property type may be any standard iOS type: id
, NSInteger
, NSUInteger
, CGFloat
, CGPoint
, CGSize
, CGRect
, UIEdgeInsets
or UIOffset
. Axis parameter values must be either NSInteger
or NSUInteger
. UIKit throws an exception if other types are used in the axes.
For example, UIBarButtonItem
defines these methods:
- (void)setTitlePositionAdjustment:(UIOffset)adjustment |
forBarMetrics:(UIBarMetrics)barMetrics UI_APPEARANCE_SELECTOR; |
- (UIImage *)backButtonBackgroundImageForState:(UIControlState)state |
barMetrics:(UIBarMetrics)barMetrics UI_APPEARANCE_SELECTOR; |
- (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage |
forState:(UIControlState)state |
barMetrics:(UIBarMetrics)barMetrics UI_APPEARANCE_SELECTOR; |
该类获取Appearance
e.g :[[[self class] appearance] hudBackgroundColor];