使用initWithImage:(UIImage *)图像将图像添加到UIBarButtonItem [英] add image to UIBarButtonItem using initWithImage:(UIImage *)image
问题描述
我想知道如何将图像设置为UIBarButtonItem,然后在创建UIBarButtonItem时使用InitWithImage将其添加到UIToolbar。
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.
我是执行以下操作,但它会创建一个空白空白,其中图像应位于UIToolbar上
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:)];
谢谢!
推荐答案
这是一个从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.
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];
这篇关于使用initWithImage:(UIImage *)图像将图像添加到UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!