如何在 iOS 7+ 上更改 UIBarButtonItem 的背景颜色? [英] How can I change the background color of a UIBarButtonItem on iOS 7+?

查看:48
本文介绍了如何在 iOS 7+ 上更改 UIBarButtonItem 的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过更改其背景颜色来指示特定的 UIBarButtonItem 是打开还是关闭.移动版 Safari 使用此功能来指示是开启还是关闭隐私浏览:

I'd like to indicate that a particular UIBarButtonItem is toggled on or off by changing its background color. Mobile Safari uses this feature to indicate whether private browsing is on or off:

我该怎么做,因为 UIBarButtonItem 上没有 backgroundColor 属性?

How can I do this, since there's no backgroundColor property on UIBarButtonItem?

推荐答案

创建一个 UIButton 并将其用作 UIBarButtonItem 的自定义视图.然后,在按钮的图层上设置backgroundColor:

Create a UIButton and use it as the custom view for the UIBarButtonItem. Then, set the backgroundColor on the button's layer:

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Test"];
button.layer.backgroundColor = [UIColor redColor].CGColor;
button.layer.cornerRadius = 4.0;

UIBarButtonItem* buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.toolbarItems = @[buttonItem];

这篇关于如何在 iOS 7+ 上更改 UIBarButtonItem 的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆