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 添加时为白色,以编程方式添加时为黑色


- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01