objective-c 我可以有一个带有彩色图像的 UIBarButtonItem 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1835260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I have a UIBarButtonItem with a colored image?
提问by DevDevDev
I have an image that I want to display on a UIBarButtonItem, but for some reason it only shows the outline of it and the rest is all white. How can I have it actually display the image?
我有一个图像,我想在 UIBarButtonItem 上显示,但由于某种原因,它只显示了它的轮廓,其余的都是白色的。我怎样才能让它实际显示图像?
Thanks!
谢谢!
回答by amrox
UPDATE:See MANIAK_dobrii's answerfor an easier solution, available in iOS 7+.
更新:请参阅MANIAK_dobrii 的答案以获得更简单的解决方案,可在 iOS 7+ 中使用。
Here is how I use an image for a UIBarButtonItem:
这是我如何为 UIBarButtonItem 使用图像:
UIImage *image = [UIImage imageNamed:@"buttonImage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
[button addTarget:myTarget action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
…
回答by MANIAK_dobrii
There's other iOS7+ solution:
还有其他iOS7+解决方案:
NSString *iconFilename = // ...
UIImage *image =
[[UIImage imageNamed:iconFilename]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *barButtonItem =
[[UIBarButtonItem alloc] initWithImage:image
style:UIBarButtonItemStylePlain
target:self
action:@selector(onBarButtonItemTapped:)];
Swift 5:
斯威夫特 5:
let iconFilename: String = // ...
let image = UIImage(named: iconFilename)?.withRenderingMode(.alwaysOriginal)
let barButtonItem = UIBarButtonItem(image: image,
style: .plain,
target: self,
action: #selector(onBarButtonItemTapped(_:)))
Extract from UIImage.h:
摘自 UIImage.h:
... navigation bars, tab bars, toolbars, and segmented controls automatically treat their foreground images as templates ... You can use UIImageRenderingModeAlwaysTemplate to force your image to always be rendered as a template or UIImageRenderingModeAlwaysOriginal to force your image to always be rendered as an original.
...导航栏、标签栏、工具栏和分段控件会自动将其前景图像视为模板...您可以使用 UIImageRenderingModeAlwaysTemplate 强制您的图像始终呈现为模板或 UIImageRenderingModeAlwaysOriginal 强制您的图像始终呈现为一个原件。
回答by Felipe Ferri
There is another way that does not involve coding at all.
还有另一种完全不涉及编码的方法。
First, place the images you want to put on the bar on the Assets.xcassets document.
首先,将要放置的图像放在 Assets.xcassets 文档的栏上。
Select the image on the assets browser.
在资产浏览器上选择图像。
Open the Attributes inspector for that image on the right vertical toolbar.
在右侧的垂直工具栏上打开该图像的属性检查器。
On "Render As" select "Original Image".
在“渲染为”上选择“原始图像”。
Even though on the storyboard the buttons will continue to be painted with the tint color, when running on the simulator the original image will be shown.
即使在情节提要上,按钮将继续使用淡色绘制,但在模拟器上运行时,将显示原始图像。
The default rendering mode for an image varies from one UI control to the other. If you set this parameter on the attributes inspector, though, you can force that an image will be always represented with a specific rendering mode.
图像的默认呈现模式因一个 UI 控件而异。但是,如果您在属性检查器上设置此参数,则可以强制图像始终以特定渲染模式表示。
If you need the same image to be represented with different rendering modes on different controllers, then the response from MANIAK_dobrii is more appropriate.
如果您需要在不同的控制器上使用不同的渲染模式来表示相同的图像,那么来自 MANIAK_dobrii 的响应更合适。
回答by Kaptain
In Swift 3:
在 Swift 3 中:
let iconname = // ...
let image = UIImage(named: iconname)?.withRenderingMode(.alwaysOriginal)
let barButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(self. onBarButtonItemTapped))
self.navigationItem.leftBarButtonItem = barButtonItem
回答by bpapa
Nope. As you can read in the Human Interface Guidelines
不。正如您在人机界面指南中所读到的
After you've decided on the appearance of your icon, follow these guidelines as you create it:
Use the PNG format. Use pure white with appropriate alpha. Do not include a drop shadow. Use anti-aliasing. If you decide to add a bevel, be sure that it is 90° (to help you do this, imagine a light source positioned at the top of the icon). For toolbar and navigation bar icons, create an icon that measures about 20 x 20 pixels. For tab bar icons, create an icon that measures about 30 x 30 pixels.
Note: The icon you provide for toolbars, navigation bars, and tab bars is used as a mask to create the icon you see in your application. It is not necessary to create a full-color icon.
确定图标的外观后,请在创建时遵循以下准则:
使用 PNG 格式。使用带有适当 alpha 的纯白色。不包括阴影。使用抗锯齿。如果您决定添加一个斜角,请确保它是 90°(为了帮助您做到这一点,请想象一个位于图标顶部的光源)。对于工具栏和导航栏图标,创建一个大小约为 20 x 20 像素的图标。对于标签栏图标,创建一个大小约为 30 x 30 像素的图标。
注意:您为工具栏、导航栏和标签栏提供的图标用作掩码来创建您在应用程序中看到的图标。没有必要创建全彩色图标。


