我试图设置图像的UIBarButtonItem我,我不能设法做到这一点。 我试了两次,在第一种情况下,我得到我的形象在正确的地方,但是当我点击按钮,没有任何反应(它应该弹出一个新窗口,但没有任何工程)。 还有就是我已经使用了一段代码:
UIImage *faceImage = [UIImage imageNamed:@"plus_button.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:faceImage style:UIBarButtonItemStylePlain target:self action:@selector(addProduct:)];
self.navigationItem.leftBarButtonItem = faceBtn;
在第二种情况下,我设置图像按钮,新窗口出现,因为它应该是,但不仅是我的自定义图像我想要的,而且它显示“边界”,它看起来就像图像被放置在默认按钮中心。 很显然,我只想要我的形象,没有国界,只有我的形象。 有我在第二情况中使用的码片:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:faceImage style:UIBarStyleDefault target:self action:@selector(addProduct:)];
self.navigationItem.leftBarButtonItem = addButton;
请帮我解决问题,任何帮助,将不胜感激,谢谢!
试试这个...改变它的方法,根据你的形象
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(10.0, 2.0, 45.0, 40.0)];
[button1 addTarget:self action:@selector(showLeft:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:@"left-arrow-button.png"] forState:UIControlStateNormal];
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithCustomView:button1];
self.navigationItem.leftBarButtonItem = button;
我已修改了代码,并测试了barbutton显示图像。 通过自己请试试这个。 并请让我知道,如果你对这个面对任何问题。
UIImage *faceImage = [UIImage imageNamed:@"Badge.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 0, 0, faceImage.size.width, faceImage.size.height );
[face addTarget:self action:@selector(showAlerts) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *faceBtn = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.leftBarButtonItem = faceBtn;
编辑:我已经加入我的回答一条线。 我只是在日志打印消息“嗨”。 这是工作。 能否请您试试吗?
谢谢。
或者只写的iOS 8斯威夫特两行代码。 (说不定还能在iOS的7工作)
let doneButtonAsLeftArrow = UIBarButtonItem(image: UIImage(named: "LeftArrow24x24.png"), style: .Plain, target: self, action: "doneButtonPushed")
navigationItem.leftBarButtonItem = doneButtonAsLeftArrow
任何人接受的答案斯威夫特版本
var settingsButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
settingsButton.frame = CGRect(origin: CGPointZero, size: CGSize(width: 36, height: 36))
settingsButton.setImage(image, forState: UIControlState.Normal)
settingsButton.addTarget(self, action: "buttonActionHere:", forControlEvents: UIControlEvents.TouchUpInside)
let rightBarButtonItem = UIBarButtonItem(customView: settingsButton)
navigationItem.setRightBarButtonItem(rightBarButtonItem, animated: true)
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:faceImage style: UIButtonTypeCustom target:self action:@selector(addProduct:)];
self.navigationItem.leftBarButtonItem = addButton;
ü正在创建一个默认的按钮,你需要创建一个UIButtonTypeCustom
型按钮...