3

I am using the appearance proxy to style my app, but I have a problem with the [UIBarButtonItem appearance]; I want to only style the buttons of the top UINavigationbar, but when I run the code below, the same style also gets applied to the done button of the keyboard.

NSDictionary *btnAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                              [UIColor blackColor],
                              UITextAttributeTextColor,
                              [UIColor clearColor],
                              UITextAttributeTextShadowColor, nil];

[[UIBarButtonItem appearance] setTitleTextAttributes: btnAttributes
                                            forState: UIControlStateNormal];

The style gets applied to the keyboards done button

So, my question is: Is it possible - using the appearance proxy - to style only bar buttons of the top navigation bar?

4

1 回答 1

4

You can restrict the appearance to certain container classes. From the appearance reference:

appearanceWhenContainedIn:

Returns the appearance proxy for the receiver in a given containment hierarchy. (required)

+ (id)appearanceWhenContainedIn:(Class <UIAppearanceContainer>)ContainerClass,...

Parameters ContainerClass, A nil-terminated list of appearance container classes. Return Value The appearance proxy for the receiver in a given containment hierarchy.

Discussion This method throws an exception for any item in the var-args list that is not a Class object that conforms to the UIAppearanceContainer protocol.

Availability Available in iOS 5.0 and later.


For your example, that would be:

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]
                     setTitleTextAttributes:btnAttributes
                                   forState:UIControlStateNormal];
于 2013-03-09T21:41:56.843 回答