定制“按下” UIBarButtonItem背景 [英] Custom "Pressed" UIBarButtonItem Backgrounds
问题描述
我正在尝试为我的UIBarButtonItem设置自定义按下的图像,但似乎没有任何效果。根据我的理解,下面的代码应该可以工作,但是尽管设置了突出显示状态的图像,按下时按钮看起来完全一样。
I'm trying to set a custom pressed image for my UIBarButtonItem but nothing seems to be working. From my understanding, the code below should work, but despite setting the image for the highlighted state, the button looks exactly the same when pressed.
任何想法?
UIImage *barButtonBackground = [[UIImage imageNamed:ANBarButtonItemBackgroundImageName] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0f,5.0f, 5.0f, 5.0f)];
UIImage *barButtonPressedBackground = [[UIImage imageNamed:ANBarButtonPressedImageName] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0f,5.0f, 5.0f, 5.0f)];
[self.navigationItem.leftBarButtonItem setBackgroundImage:barButtonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:barButtonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.leftBarButtonItem setBackgroundImage:barButtonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:barButtonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
推荐答案
我认为你应该创建自定义UIButton
背景图片,用于与您的配色方案匹配的不同控件状态,然后使用此 UIButton
作为自定义UIBarButtonItem的视图
。
I think you should 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:...];
[customButton setBackgroundImage:barButtonBackground forState:UIControlStateNormal];
[customButton setBackgroundImage:barButtonPressedBackground forState:UIControlStateSelected];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView: customButton];
如果您想要其他参考资料,那么您可以浏览这个漂亮的链接:使用外观代理来设置样式应用
If you want some other Reference then you can go through this beautiful link : Using Appearance Proxy to Style Apps
这篇关于定制“按下” UIBarButtonItem背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!