I’m styling my UINavigationBar with UIAppearance. I want all the back buttons to have gray text and all my rightBarButtonItems to have green 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.
rightBarButtonItem
UIBarButtonItem* done = [[ActionBarButtonItem alloc]
initWithTitle:@"Done"
style:UIBarButtonItemStyleDone
target:self
action:@selector(onDonePress)];
self.navigationItem.rightBarButtonItem = done;
[done release];
UIAppearance
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
];
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, every UIBarButtonItem gets green text. Why?


You are sub classing
UIBarButtonso my initial thought here is that when you callappearanceWhenContainedInonActionBarButtonItemthe result is a call on the super classesappearanceWhenContainedInand therefore this is why this is happening. It is not ideal but you could change the appearance of the left and right items on the view did load or view will appear in each view.You may also choose to put this somewhere convent like your app delegate so you can access it more simply with
[[UIApplication sharedApplication].delegate setBarButtonColors].Another alternative could be a category on
UINavigationBar.