1

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.

4

1 回答 1

0

我认为文档中的这一段将帮助您:

在任何给定的视图层次结构中,最外层的外观代理获胜。特异性(链的深度)是决胜局。换句话说,appearanceWhenContainedIn: 中的包含语句被视为部分排序。给定一个具体的排序(实际的子视图层次结构),UIKit 在从窗口向下读取实际层次结构时选择作为第一个唯一匹配的部分排序。

UIAppearance 协议参考

于 2013-11-21T16:00:17.130 回答