4

我正在用UIAppearance 为我的 UINavigationBar 设计样式。我希望所有后退按钮都有灰色文本,我的所有 rightBarButtonItems 都有绿色文本(颜色是假设的)。由于默认情况下这两个按钮都是 UIBarButtonItems,因此 UIAppearance 将无法区分两者。所以我决定继承一个 UIBarButtonItem,称之为 ActionBarButtonItem。我在需要 rightBarButtonItem 的任何地方使用这个新的子类。

rightBarButtonItem

UIBarButtonItem* done = [[ActionBarButtonItem alloc] 
    initWithTitle:@"Done"
    style:UIBarButtonItemStyleDone
    target:self 
    action:@selector(onDonePress)];
self.navigationItem.rightBarButtonItem = done;
[done release];

UI外观

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
];

理论上,灰色文本将应用于所有 UIBarButtonItem。然后我只为 ActionBarButtonItems 用绿色文本覆盖灰色文本。最终结果并不如预期。由于未知原因,每个UIBarButtonItem 都会获得绿色文本。为什么?

在此处输入图像描述在此处输入图像描述

4

1 回答 1

1

你是子类UIBarButton,所以我最初的想法是,当你调用appearanceWhenContainedIn结果ActionBarButtonItem是调用超类时appearanceWhenContainedIn,这就是发生这种情况的原因。这并不理想,但您可以更改加载视图上左右项目的外观,或者视图将出现在每个视图中。

NSDictionary* buttonStyle = [NSDictionary 
                                 dictionaryWithObjects:[NSArray 
                                                        arrayWithObjects:
                                                        [UIColor blueColor],nil]
                                 forKeys:[NSArray
                                          arrayWithObjects:
                                          UITextAttributeTextColor,
                                          nil
                                          ]
                                 ];

NSDictionary* actionStyle = [NSDictionary 
                             dictionaryWithObjects:[NSArray 
                                                    arrayWithObjects:
                                                    [UIColor greenColor],
                                                    nil
                                                    ]
                             forKeys:[NSArray
                                      arrayWithObjects:
                                      UITextAttributeTextColor,
                                      nil
                                      ]
                             ]; 



[self.navigationItem.leftBarButtonItem setTitleTextAttributes:buttonStyle forState:UIControlStateNormal];
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:actionStyle forState:UIControlStateNormal];

你也可以选择把它放在某个地方,比如你的应用程序委托,这样你就可以更简单地使用[[UIApplication sharedApplication].delegate setBarButtonColors]. 另一种选择可能是UINavigationBar.

于 2012-06-13T23:49:04.953 回答