Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant set image for UIBarButtonItem

I'm trying to set image for my UIBarButtonItem and I can't manage to do that. I tried two times, and in first case I get my image in right place, but when I click on button nothing happens (it should pop out a new window, but nothing works). There is the piece of code I have 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 second case, I set image on button and new window appears as it should be, but there is not only my custom image I want, but also it show "borders", it looks like image was put in center on default button. Obviously I want only my image, not borders, only my image. There is piece of code I have used in second case:

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

    self.navigationItem.leftBarButtonItem = addButton;

Please help me to solve the problem, any help would be appreciated, thank you!

like image 315
Evgeniy Kleban Avatar asked Jun 30 '12 12:06

Evgeniy Kleban


4 Answers

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

    let doneButtonAsLeftArrow = UIBarButtonItem(image: UIImage(named: "LeftArrow24x24.png"), style: .Plain, target: self, action: "doneButtonPushed")
    navigationItem.leftBarButtonItem = doneButtonAsLeftArrow
like image 198
clearlight Avatar answered Nov 09 '22 21:11

clearlight


try this ... change it's method, image 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;
like image 9
TheTiger Avatar answered Nov 09 '22 22:11

TheTiger


I have modified your code and tested the image showing in the barbutton. Please try this by yourself. And please let me know if you face any problem on this.

    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 have added a line in my answer. I just printed a message "Hi" in log. it is working. Can you please try it?

Thanks.

like image 3
Yuvaraj.M Avatar answered Nov 09 '22 21:11

Yuvaraj.M


Swift version of 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)
like image 1
David H. Avatar answered Nov 09 '22 20:11

David H.