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 添加时为白色,以编程方式添加时为黑色
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01