Failed to set image for UIBarButtonItem

I am trying to set an image for my UIBarButtonItem and I cannot do this. I tried twice, and in the first case I get the image in the right place, but when I click the button, nothing happens (it should pop out of a new window, but nothing works). There is a piece of code that I used:

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; 

In the second case, I set the image on the button, and a new window appears, as it should be, but not only is my custom image that I want, but it also shows β€œborders”, it looks like the image was placed in the center by default button . Obviously, I want only my image, not borders, only my image. In the second case, a code snippet is used:

 UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:faceImage style:UIBarStyleDefault target:self action:@selector(addProduct:)]; self.navigationItem.leftBarButtonItem = addButton; 

Please help me solve the problem, any help would be appreciated, thanks!

+7
source share
5 answers

try this ... change its method, the image is according to you

 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; 
+8
source

I changed your code and tested the image shown in the bar. Try it yourself. And please let me know if you have any problems.

  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; 

EDIT: I added a line to my answer. I just printed a "Hello" message in a magazine. it works. Could you try it?

Thanks.

+3
source

Or ... just write two lines of iOS 8 Swift code. (I can even work in iOS 7)

  let doneButtonAsLeftArrow = UIBarButtonItem(image: UIImage(named: "LeftArrow24x24.png"), style: .Plain, target: self, action: "doneButtonPushed") navigationItem.leftBarButtonItem = doneButtonAsLeftArrow 
+3
source
 UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:faceImage style: UIButtonTypeCustom target:self action:@selector(addProduct:)]; self.navigationItem.leftBarButtonItem = addButton; 

u created the default button, you need to create a button of type UIButtonTypeCustom ...

+1
source

A quick version of the accepted answer for anyone

 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) 
0
source

All Articles