UIBarButtonItem突出显示的颜色 [英] UIBarButtonItem Highlighted Color
问题描述
我为 UINavigationBar
(在 UINavigationController
内)设置了自定义色调颜色,插入到 UINavigationBar
中的 UIBarButtonItems
的适当匹配颜色。然而,当我选择一个 UIBarButtonItem
按钮变成(大概)突出显示的状态,并呈现一种不同的颜色,看起来相当有点,不能很好地匹配的色彩颜色。有没有办法改变这个突出显示的状态颜色为自定义颜色?
理想情况下,我想只是创建一个类别 UIBarButtonItem
,这会更改
UIBarButtonItem
的所有实例的突出显示的颜色,因为这将避免需要显式子类化 UIBarButtonItems
然后更改我的应用程序中的每个引用使用子类(这将是棘手的,因为我使用一些第三方库只是使用 UIBarButtonItem
任何帮助将非常感谢。从我记得面对一个类似的问题,UINavigationBar将只是采取tintColor和使它更暗的UIBarButtonItem(除非风格设置为BarStyleBlack,在这种情况下,它使它成为一个暗灰色)。
要做你想要的,我将创建一个自定义的UIButton与背景图像为不同的控制状态匹配您的配色方案,然后使用此UIButton作为视图自定义UIBarButtonItem。
UIButton * customButton = [UIButton buttonWithType:...]
//normal_button.png和selected_button.png需要由你创建
[customButton setBackgroundImage:[UIImage imageNamed:@normal_button.png] forState:UIControlStateNormal];
[customButton setBackgroundImage:[UIImage imageNamed:@selected_button.png] forState:UIControlStateSelected];
UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
如果你想尝试并封装这个,你总是可以创建一个工厂或自定义init方法UIBarButtonItem(通过类别),并使用上面的代码(稍作修改)。
我知道我没有完全解决你的第二点只是覆盖控制状态与类别。我不知道什么方法重写在UIBarButtonItem做这样的事情,但你可以能够通过方法swizzling(http://cocoadev.com/index.pl?MethodSwizzling)一旦你找到了哪个
我应该注意,我只使用swizzling进行测试/调试。
I have set a custom tint color for a UINavigationBar
(within a UINavigationController
) which, in turn, sets an appropriate matching color for the UIBarButtonItems
which are inserted into the UINavigationBar
. However, when I select a UIBarButtonItem
the button turns into (presumably) the highlighted state and presents a different color, which looks quite a bit out and does not nicely match the tint color. Is there a way to change this highlighted state color to a custom color?
Ideally, I would like to just create a category on UIBarButtonItem
which changes the highlighted color for all instances of UIBarButtonItem
, as this would avoid the need to explicitly subclass UIBarButtonItems
and then change every reference in my app to use the subclass (which will be tricky, as I am using some third-party libraries which just use UIBarButtonItem
and I don't want to go messing with their implementation).
Any help would be greatly appreciated.
From what I remember from facing a similar issue, UINavigationBar will just take the tintColor and make it darker for the UIBarButtonItem (unless the style is set to BarStyleBlack, in which case it makes it a dull gray).
To do what you ask, I would create a custom UIButton with background images for the different control states that match your color scheme, then use this UIButton as the view for a custom UIBarButtonItem.
UIButton *customButton = [UIButton buttonWithType:...];
//normal_button.png and selected_button.png need to be created by you
[customButton setBackgroundImage: [UIImage imageNamed:@"normal_button.png"] forState:UIControlStateNormal];
[customButton setBackgroundImage: [UIImage imageNamed:@"selected_button.png"] forState:UIControlStateSelected];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView: customButton];
If you want to try and encapsulate this, you could always create a factory or a custom init method on UIBarButtonItem (via a category) and use the above code (with slight modifications).
I am aware that I am not fully addressing your second point on just overriding the control state with a category. I don't know what method to override in UIBarButtonItem to do such a thing, but you may be able to accomplish what you want via method swizzling (http://cocoadev.com/index.pl?MethodSwizzling) once you find out which method you want to exchange.
I should note that I've only ever used swizzling for testing/debugging.
这篇关于UIBarButtonItem突出显示的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!