我可以有一个带有彩色图像的 UIBarButtonItem 吗?

发布于 2024-08-13 20:26:15 字数 90 浏览 1 评论 0原文

我有一个图像,我想在 UIBarButtonItem 上显示,但由于某种原因它只显示它的轮廓,其余部分都是白色的。我怎样才能让它真正显示图像?

谢谢!

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?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月亮邮递员 2024-08-20 20:26:15

还有其他 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:

let iconFilename: String = // ...
let image = UIImage(named: iconFilename)?.withRenderingMode(.alwaysOriginal)
let barButtonItem = UIBarButtonItem(image: image, 
                                    style: .plain, 
                                    target: self, 
                                    action: #selector(onBarButtonItemTapped(_:)))

从 UIImage.h 中提取:

...导航栏、选项卡栏、工具栏和分段控件自动将其前景图像视为模板...您可以使用 UIImageRenderingModeAlwaysTemplate 强制您的图像始终呈现为模板,或使用 UIImageRenderingModeAlwaysOriginal 强制您的图像始终呈现为模板被渲染为原件。

There's other iOS7+ solution:

NSString *iconFilename = // ...
UIImage *image = 
    [[UIImage imageNamed:iconFilename] 
        imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *barButtonItem = 
    [[UIBarButtonItem alloc] initWithImage:image
                                     style:UIBarButtonItemStylePlain 
                                    target:self 
                                    action:@selector(onBarButtonItemTapped:)];

Swift 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:

... 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.

回忆躺在深渊里 2024-08-20 20:26:15

更新:请参阅 MANIAK_dobrii 的答案,获取更简单的解决方案,可在 iOS 7+ 中使用。


以下是我如何将图像用于 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];
…

UPDATE: See MANIAK_dobrii's answer for an easier solution, available in iOS 7+.


Here is how I use an image for a 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];
…
卸妝后依然美 2024-08-20 20:26:15

还有另一种根本不涉及编码的方法。

首先,将要放置在 Assets.xcassets 文档上的栏上的图像。

在资源浏览器中选择图像。

选择图像

在右侧垂直工具栏上打开该图像的属性检查器。

属性检查器

在“渲染为”上选择“原始图像”。

原始图片

尽管在故事板上按钮将继续使用色调颜色,但在模拟器上运行时将显示原始图像。

模拟器

图像的默认渲染模式因 UI 控件而异。不过,如果您在属性检查器上设置此参数,则可以强制图像始终以特定的渲染模式表示。

如果您需要在不同的控制器上使用不同的渲染模式表示相同的图像,那么来自 MANIAK_dobrii 的响应更合适。

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.

Select the image on the assets browser.

Select the image

Open the Attributes inspector for that image on the right vertical toolbar.

Attributes inspector

On "Render As" select "Original Image".

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.

Simulator

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.

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.

扎心 2024-08-20 20:26:15

在 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

In 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
小帐篷 2024-08-20 20:26:15

没有。正如您可以在 人机界面指南

决定图标的外观后,请在创建图标时遵循以下准则:

使用 PNG 格式。
使用具有适当 alpha 的纯白色。
不要包含投影。
使用抗锯齿。
如果您决定添加斜角,请确保其为 90°(为了帮助您做到这一点,请想象一个位于图标顶部的光源)。
对于工具栏和导航栏图标,创建一个大小约为 20 x 20 像素的图标。
对于选项卡栏图标,创建一个尺寸约为 30 x 30 像素的图标。

注意:您为工具栏、导航栏和选项卡栏提供的图标用作遮罩来创建您在应用程序中看到的图标。
没有必要创建全彩图标。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文