当通过IB添加时,UIBarButtonItem图标为白色,以编程方式添加时为黑色 [英] UIBarButtonItem icon white when added via IB, black when added programmatically

查看:192
本文介绍了当通过IB添加时,UIBarButtonItem图标为白色,以编程方式添加时为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过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:message。

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.

这篇关于当通过IB添加时,UIBarButtonItem图标为白色,以编程方式添加时为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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