UIBarButtonItem icon white when added via IB, black when added programmatically(UIBarButtonItem 图标通过 IB 添加时为白色,以编程方式添加时为黑色)
问题描述
当我通过 Interface Builder 向 UIBarButtonItem 添加图标时,图标显示为白色.当我以编程方式将相同的图标文件添加到另一个 UIToolbar 时,图标显示为黑色.为什么?
When I add an icon to a UIBarButtonItem via the Interface Builder, the icon is displayed white. When I add the same icon file programmatically to another UIToolbar, the icon is displayed black. Why?
UIImage *image = [UIImage imageNamed:@"icon.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
rootViewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:reloadButton] autorelease];
推荐答案
Jongsma 说的都对,你应该使用 initWithImage:style: 消息.
Everything Jongsma said is right, you should use the initWithImage:style: message.
下一个问题不是您创建 UIBarButtonItem 的方式,而是您分配它的位置.您使用 UIBarButtonItemStylePlain 创建它,它通常应该将图标的轮廓呈现为白色,但 UINavigationItem 的 rightBarButtonItem(就像左侧一样)不允许使用 UIBarButtonItemStylePlain.它被隐式转换为 UIBarButtonItemStyleBordered.在带边框的样式中,图标按原样"呈现,即带有轻微渐变的黑色.
The next problem is not the way you create the UIBarButtonItem, but the place you assign it. You create it with UIBarButtonItemStylePlain, which should normally render the icon's outline in white, but the rightBarButtonItem of a UINavigationItem (just like the left) is not allowed the UIBarButtonItemStylePlain. It's implicitly converted to UIBarButtonItemStyleBordered. In the bordered style the icon is rendered 'as is', which is black with a slight gradient.
我认为,如果您希望在有边框的 barButton 上显示白色项目,则必须触摸图像本身.
I think if you want the item in white on a bordered barButton, you'll have to touch the image itself.
这篇关于UIBarButtonItem 图标通过 IB 添加时为白色,以编程方式添加时为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIBarButtonItem 图标通过 IB 添加时为白色,以编程方式添加时为黑色
基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
