无法为UIBarButtonItem设置图像
我试图为我的UIBarButtonItem设置图像,我无法做到这一点。 我尝试了两次,第一种情况是我的图像在正确的位置,但是当我点击button时,没有任何反应(它应该popup一个新窗口,但没有任何效果)。 有一段我使用的代码:
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;
在第二种情况下,我在button上设置图像,新的窗口应该是出现的,但是不仅我想要的是我的自定义图像,而且还显示“边框”,看起来像图像放在默认button的中心。 显然,我只想要我的形象,而不是边界,只有我的形象。 在第二种情况下我使用了一段代码:
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;
我修改了你的代码,并testing了在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;
编辑:我在我的答案中添加了一行。 我只是在日志里打印一条消息“Hi”。 这是工作。 你可以试试吗?
谢谢。
或者…只写两行iOS 8 Swift代码。 (甚至可能在iOS 7中工作)
let doneButtonAsLeftArrow = UIBarButtonItem(image: UIImage(named: "LeftArrow24x24.png"), style: .Plain, target: self, action: "doneButtonPushed") navigationItem.leftBarButtonItem = doneButtonAsLeftArrow
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:faceImage style: UIButtonTypeCustom target:self action:@selector(addProduct:)]; self.navigationItem.leftBarButtonItem = addButton;
你正在创build一个默认button,你需要创build一个UIButtonTypeCustom
types的button…
任何人接受答案的迅捷版本
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)