I'm trying to customize a UIBarButtonItem while it's embedded in a UINavigationBar.
I would like to customize the background image as well as the title text attributes.
However, in case the navigation bar is contained in a UIPopoverController, I don't want these customizations to take place.
This is my Code:
//1.
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]
setBackgroundImage:[UIImage imageNamed:@"navigationBarBackgroundNormal"]
forState:UIControlStateNormal
forBarMetrics:UIBarMetricsDefault];
//2.
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]
setBackgroundImage:[UIImage imageNamed:@"navigationBarBackgroundHighlighted"]
forState:UIControlStateHighlighted
forBarMetrics:UIBarMetricsDefault];
//3.
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]
setTitleTextAttributes:@{UITextAttributeTextColor:[UIColor blueColor]}
forState:UIControlStateNormal];
//4.
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]
setTitleTextAttributes:@{UITextAttributeTextColor:[UIColor whiteColor]}
forState:UIControlStateHighlighted];
//5.
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], [UIPopoverController class], nil]
setBackgroundImage:nil
forState:UIControlStateNormal
forBarMetrics:UIBarMetricsDefault];
//6.
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], [UIPopoverController class], nil]
setBackgroundImage:nil
forState:UIControlStateHighlighted
forBarMetrics:UIBarMetricsDefault];
//7.
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], [UIPopoverController class], nil]
setTitleTextAttributes:nil
forState:UIControlStateNormal];
//8.
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], [UIPopoverController class], nil]
setTitleTextAttributes:nil
forState:UIControlStateHighlighted];
The problem is:
I create a navigation controller as the contentViewController of a popover.
When another view controller is pushed, a back button is automatically created and the appearance which applies is from lines 5,6,7,8 - which is good.
However, when I create a new UIBarButtonItem and assign it as the rightBarButtonItem, lines 3,4,5,6 are applied instead (meaning, the appearance proxy, for some reason, applied the title attributes from lines 3,4, even though the navigation bar is in a popover. Yet when it comes to the background image, the appearance proxy correctly applied lines 5,6)
When a navigation bar isn't in a popover, everything works just fine (lines 1,2,3,4 are applied).
I can't figure out why the appearance proxy works as it should for the back button, but not for the UIBarButtonItem I'm creating, when the navigation controller is in a popover.