Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use UIAppearance to style a subclass but not the superclass?
    text
    copied!<p>I'm styling my UINavigationBar with <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html" rel="nofollow noreferrer">UIAppearance</a>. I want all the back buttons to have <strong>gray</strong> text and all my rightBarButtonItems to have <strong>green</strong> text (colors are hypothetical). Since both buttons are UIBarButtonItems by default, UIAppearance would not be able to differentiate the two. So I decided to subclass a UIBarButtonItem, calling it ActionBarButtonItem. I use this new subclass anywhere I need a rightBarButtonItem.</p> <h2>rightBarButtonItem</h2> <pre><code>UIBarButtonItem* done = [[ActionBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(onDonePress)]; self.navigationItem.rightBarButtonItem = done; [done release]; </code></pre> <h2>UIAppearance</h2> <pre><code>NSDictionary* buttonStyle = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: [UIColor grayColor], , nil ] forKeys:[NSArray arrayWithObjects: UITextAttributeTextColor, nil ] ]; NSDictionary* actionStyle = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: [UIColor greenColor], nil ] forKeys:[NSArray arrayWithObjects: UITextAttributeTextColor, nil ] ]; [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil] setTitleTextAttributes:buttonStyle forState:UIControlStateNormal ]; [[ActionBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil] setTitleTextAttributes:actionStyle forState:UIControlStateNormal ]; </code></pre> <p>Theoretically, the gray text would be applied for all UIBarButtonItems. Then I override that gray text with green text for ActionBarButtonItems only. The final result is not as expected. For unknown reasons, <em>every</em> UIBarButtonItem gets green text. Why?</p> <p><img src="https://i.stack.imgur.com/2Lc10.png" alt="enter image description here"><img src="https://i.stack.imgur.com/m0oZA.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload