UIAppearance的使用

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 of UINavigationBar:

    [[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: idNSIntegerNSUIntegerCGFloatCGPointCGSizeCGRectUIEdgeInsets 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];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值