使用 initWithImage 将图像添加到 UIBarButtonItem:(UIImage *)image

发布于 2024-09-10 23:22:46 字数 373 浏览 4 评论 0原文

我想知道如何在创建 UIBarButtonItem 时使用 InitWithImage 将图像设置为 UIBarButtonItem,然后将其添加到 UIToolbar。

我正在执行以下操作,但它会在图像应位于 UIToolbar 上的位置创建一个空白,

UIImage *image = [UIImage imageNamed:@"6.png"];

UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(pp:)];

谢谢!

I would like to know how to set an image to a UIBarButtonItem which will then be added to a UIToolbar, by using InitWithImage when creating a UIBarButtonItem.

I am doing the following, but it creates a blank whitespace where the image should be on the UIToolbar

UIImage *image = [UIImage imageNamed:@"6.png"];

UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(pp:)];

Thank You!

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

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

发布评论

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

评论(10

找回味觉 2024-09-17 23:22:46

对于 iOS 7+,您执行以下操作:

Objective-C

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

Swift 2.3

 let image = UIImage(named: "myImage")?.imageWithRenderingMode(.AlwaysOriginal)
 let button = UIBarButtonItem(image: image, style: .Plain, target: self, action: #selector(YOUR_METHOD(_:)))

Swift 3.0

let image = UIImage(named: "myImage")?.withRenderingMode(.alwaysOriginal)
let button = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(YOUR_METHOD(_:)))

And for iOS 7+ you do the following:

Objective-C

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

Swift 2.3

 let image = UIImage(named: "myImage")?.imageWithRenderingMode(.AlwaysOriginal)
 let button = UIBarButtonItem(image: image, style: .Plain, target: self, action: #selector(YOUR_METHOD(_:)))

Swift 3.0

let image = UIImage(named: "myImage")?.withRenderingMode(.alwaysOriginal)
let button = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(YOUR_METHOD(_:)))
清音悠歌 2024-09-17 23:22:46

下面是一个从 Facebook 徽标创建工具栏按钮的示例。
创建指向图像的指针(文件已添加到 xCode),创建自定义按钮,更改按钮的大小以匹配图像,设置按钮图像,从按钮视图创建工具栏按钮。

UIImage *faceImage = [UIImage imageNamed:@"facebook.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 0, 0, faceImage.size.width, faceImage.size.height );
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *faceBtn = [[UIBarButtonItem alloc] initWithCustomView:face];

Here's an example that creates a toolbar button from the Facebook logo.
Create a pointer to an image (file already added to xCode), create a custom button, change the size of the button to match the image, set image of button, create toolbar button from button view.

UIImage *faceImage = [UIImage imageNamed:@"facebook.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 0, 0, faceImage.size.width, faceImage.size.height );
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *faceBtn = [[UIBarButtonItem alloc] initWithCustomView:face];
天涯沦落人 2024-09-17 23:22:46

我会尝试设置

UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"] style:UIBarButtonItemStylePlain target:self action:@selector(pp:)];

I would try to set

UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"] style:UIBarButtonItemStylePlain target:self action:@selector(pp:)];
壹場煙雨 2024-09-17 23:22:46

来自 UIBarButtonItem 文档

栏上显示的图像源自该图像。如果该图像太大而无法放在条上,则会缩放以适合该图像。通常,工具栏和导航栏图像的大小为 20 x 20 点。源图像中的 Alpha 值用于创建图像 - 不透明值将被忽略。

换句话说,UIBarButton 项目将忽略图像的颜色并根据当前的栏样式重新着色。它显示图像的唯一方法是使用不同的 alpha 值绘制图像。由于图像的所有像素的 alpha 值为 1,因此它们都以最大亮度绘制,并且您会得到一个空白区域。

From the documentation of UIBarButtonItem:

The images displayed on the bar are derived from this image. If this image is too large to fit on the bar, it is scaled to fit. Typically, the size of a toolbar and navigation bar image is 20 x 20 points. The alpha values in the source image are used to create the images—opaque values are ignored.

In other words, the UIBarButton item is going to disregard the color of your image and recolor it according to the current bar style. The only way it will display an image is if the image is drawn using varying alpha values. Since all the pixels of your image have an alpha value of 1, they are all drawn at maximum brightness and you get a white space.

水波映月 2024-09-17 23:22:46

Swift 中:

第一个解决方案

let img = UIImage(named: "picture")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let leftBarButtonItem = UIBarButtonItem(image: img, style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navigationItem.leftBarButtonItem = leftBarButtonItem

第二个解决方案

let img = UIImage(named: "picture")!
let imgWidth = img.size.width
let imgHeight = img.size.height
let button = UIButton(frame: CGRect(x: 0, y: 0, width: imgWidth, height: imgHeight))
button.setBackgroundImage(img, forState: .Normal)
button.addTarget(self, action: nil, forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)

In Swift:

First solution

let img = UIImage(named: "picture")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let leftBarButtonItem = UIBarButtonItem(image: img, style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navigationItem.leftBarButtonItem = leftBarButtonItem

Second solution

let img = UIImage(named: "picture")!
let imgWidth = img.size.width
let imgHeight = img.size.height
let button = UIButton(frame: CGRect(x: 0, y: 0, width: imgWidth, height: imgHeight))
button.setBackgroundImage(img, forState: .Normal)
button.addTarget(self, action: nil, forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
一身骄傲 2024-09-17 23:22:46

创建栏按钮项目后,执行下一步:

systemItem1.image = [systemItem1.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

After you create your bar button item do next:

systemItem1.image = [systemItem1.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
烏雲後面有陽光 2024-09-17 23:22:46

我也遇到了这个问题,并通过使用带有 alpha 通道的 png 文件解决了它。
我试图做一个快速的模型,并在背景上添加一个带有 jpg 文件的后退按钮(按钮的默认颜色)。我有一个白色的矩形。但是当我在透明背景上创建一个箭头并将其保存为 png 文件时,它起作用了。

I was having this problem too and solved it by using a png file with an alpha channel.
I was trying to do a quick mockup and added a back button with a jpg file on a background the default colour of the buttons. I got a white rectangle. But when I created an arrow on a transparent background and saved it as a png file, it worked.

输什么也不输骨气 2024-09-17 23:22:46

UIBarbuttonItem initWithImage 不会使用指定的 Image 创建栏按钮。
它使用给定的图像创建 UIBarbutton 的图案。如果指定的图像不透明,它将显示一个带有指定色调的条形按钮,该按钮具有图像的大小。
如果图像有不透明部分,则色调颜色将填充在不透明部分,如默认的 UIBarButtonSystemItemBookmarks。它具有不透明边框和透明内侧。

在你的情况下,titncolor是白色的,这就是它显示白色的原因。

The UIBarbuttonItem initWithImage will not create a bar button with that specified Image.
It creates a pattern of a UIBarbutton using the given image. If the specified image is not transparent it will show a bar button with specified tintcolor on it which has the size of the image.
If the image has opaque parts then the tint color will be filled on the opaque parts like the default UIBarButtonSystemItemBookmarks.It hase opaque boarder and transparent inner side.

In your case the titncolor is white that is why it shows white.

飞烟轻若梦 2024-09-17 23:22:46

我会写和你一样的代码。请注意,当您将图像添加到项目时,请确保将其添加到您正在构建的目标中。

I would write the same code as you do. Note when you add image to project, make sure it is added to the target you are building against.

↘紸啶 2024-09-17 23:22:46

您可以使用系统标准图标<创建UIBarButtonItem /a> 使用
- initWithBarButtonSystemItem:target:action:

例如:

[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(YOUR_METHOD:)];

You are able to create UIBarButtonItem with system standard icons using
- initWithBarButtonSystemItem:target:action:

For example:

[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(YOUR_METHOD:)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文