使用 initWithImage 将图像添加到 UIBarButtonItem:(UIImage *)image
我想知道如何在创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
对于 iOS 7+,您执行以下操作:
Objective-C
Swift 2.3
Swift 3.0
And for iOS 7+ you do the following:
Objective-C
Swift 2.3
Swift 3.0
下面是一个从 Facebook 徽标创建工具栏按钮的示例。
创建指向图像的指针(文件已添加到 xCode),创建自定义按钮,更改按钮的大小以匹配图像,设置按钮图像,从按钮视图创建工具栏按钮。
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.
我会尝试设置
I would try to set
来自 UIBarButtonItem 文档:
换句话说,UIBarButton 项目将忽略图像的颜色并根据当前的栏样式重新着色。它显示图像的唯一方法是使用不同的 alpha 值绘制图像。由于图像的所有像素的 alpha 值为 1,因此它们都以最大亮度绘制,并且您会得到一个空白区域。
From the documentation of UIBarButtonItem:
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.
在 Swift 中:
第一个解决方案
第二个解决方案
In Swift:
First solution
Second solution
创建栏按钮项目后,执行下一步:
After you create your bar button item do next:
我也遇到了这个问题,并通过使用带有 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.
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.
我会写和你一样的代码。请注意,当您将图像添加到项目时,请确保将其添加到您正在构建的目标中。
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.
您可以使用系统标准图标<创建
UIBarButtonItem
/a> 使用- initWithBarButtonSystemItem:target:action:
例如:
You are able to create
UIBarButtonItem
with system standard icons using- initWithBarButtonSystemItem:target:action:
For example: