UIBarButtonItem 图像未显示,而是显示图像大小的白色矩形,为什么?

发布于 2024-09-03 05:13:27 字数 116 浏览 4 评论 0 原文

无论我尝试初始化 UIBarButtonItem 的任何图像,它都只是显示图像大小的白色背景。即使我厌倦了界面构建器,结果也是一样的。所有这些图像与其他对象一起使用时效果都很好。

我该如何解决这个问题?

With whatever image I try to intialize the UIBarButtonItem, its just showing a white background in the size of the image. Even when I tired in interface builder, the result is the same. All these images when used with other objects works perfectly.

How can I solve this??

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

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

发布评论

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

评论(3

攒眉千度 2024-09-10 05:13:27

仅使用图像中的 alpha 值来创建栏按钮图像。无论您提供什么图像,都会根据 Alpha 值转换为带有白色阴影的图像。

因此,考虑到您的图像是完全白色的,很明显您没有任何透明度。

指南有这样的说法:

  • 使用 PNG 格式。
  • 使用具有适当 Alpha 值的纯白色。
  • 请勿包含投影。
  • 使用抗锯齿功能。
  • 如果您决定添加斜角,请确保它是 90°(以帮助您完成
    这个,想象一个光源
    位于图标顶部)。
  • 对于工具栏和导航栏图标,创建一个可测量的图标
    大约 20 x 20 像素。
  • 对于标签栏图标,请创建一个尺寸约为 30 x 30 像素的图标。

注意:您提供的图标
工具栏、导航栏和选项卡
条形用作掩模来创建
您在应用程序中看到的图标。它
没有必要创建一个
全彩图标。

但是,您可以使用自定义视图来获取全彩图像,如以下问题所示:

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

这有点过头了,最好遵循指南并使用普通按钮格式适当的图像。

Only alpha values in the image are used to create the bar button image. Whatever image you provide is converted into a image with shades of white, based on the alpha values.

So given that your image is completely white it is clear you don't have any transparency.

The guidelines have this to say:

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

You can however use a custom view to get a full-colour image as this question shows:

Can I have a UIBarButtonItem with a colored image?

This is a bit over the top though and it would be best to stick to the guidelines and use a normal button with an appropriately formatted image.

夜未央樱花落 2024-09-10 05:13:27

在 ios 7 及以上版本中这会有所帮助

UIImage *image = [[UIImage imageNamed:@"myImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];

In ios 7 onwards this will help

UIImage *image = [[UIImage imageNamed:@"myImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];
倾城泪 2024-09-10 05:13:27

在 Swift 3 中
我发现以下工作 - 这里显示为多个 barButtonItem 的数组

  let barButtonItem1    = UIBarButtonItem( image: UIImage(named: "myImage")?.withRenderingMode(.alwaysOriginal) , style: .plain ,target: self, action: #selector(yourAction))
  let barButtonItem2    = UIBarButtonItem(image: UIImage(named: "myImage")?.withRenderingMode(.alwaysOriginal)  , style: .plain, target: self, action: #selector(yourAction))

navigationItem.rightBarButtonItems = [barButtonItem1, barButtonItem2]

这在右侧显示了 x2 barButtons。
要显示左侧,只需更改为

navigationItem.leftBarButtonItem = [barButtonItem1, barButtonItem2]

In Swift 3
I found that the following works - here shown as an array of more than one barButtonItem

  let barButtonItem1    = UIBarButtonItem( image: UIImage(named: "myImage")?.withRenderingMode(.alwaysOriginal) , style: .plain ,target: self, action: #selector(yourAction))
  let barButtonItem2    = UIBarButtonItem(image: UIImage(named: "myImage")?.withRenderingMode(.alwaysOriginal)  , style: .plain, target: self, action: #selector(yourAction))

navigationItem.rightBarButtonItems = [barButtonItem1, barButtonItem2]

This shows x2 barButtons on the rightHandSide.
To shown the left simply change to

navigationItem.leftBarButtonItem = [barButtonItem1, barButtonItem2]

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