17

I have added UITabButtonItem onto my view controller using following code. Could anyone please tell me if its possible to associate image with it?

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(favouriteButtonClicked:)]; 
viewController2.navigationItem.rightBarButtonItem = button; 
[button release];
[self.navigationController pushViewController:viewController2 animated:YES];

I use following code at other places but as in above code I am pushing viewcontroller onto my current view, this code doesn't work. I had to use above code.

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 25, 30, 30)];
viewController2.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

[self.navigationController pushViewController:viewController2 animated:YES];

I don't know what is wrong but when I replace this custom button code with initWithBarButtonSystemItem, it works fine!!!

Finally I sorted out this issue and posted working code here but still I don't understand if I have a button in baseviewcontroller and I add it like following, it doesn't work!!! I had to use the code given in my accepted answer in viewwillappear!!

    [favButton addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
     self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];  

Following code still doesn't work!!! Don't know what's going on.

MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:filePath]] autorelease];

UIButton* favouritebutton =  [UIButton buttonWithType:UIButtonTypeCustom];
[favouritebutton setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[favouritebutton addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[favouritebutton setFrame:CGRectMake(280, 25, 30, 30)];
videoController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:favouritebutton] autorelease]; 

[self presentMoviePlayerViewControllerAnimated:videoController];

Thanks.

Paresh Masani
  • 7,474
  • 12
  • 73
  • 139

7 Answers7

21
UIButton *favButton = [[UIButton alloc] init];

[favButton setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[favButton addTarget:self action:@selector(favouriteButtonClicked:)
   forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *button = [[UIBarButtonItem alloc]
                                initWithCustomView:favButton];

self.navigationItem.rightBarButtonItem = button;

[button release];
[favButton release];
Maggie
  • 7,823
  • 7
  • 45
  • 66
  • I am not adding button in current view(self). I am trying to add in on viewcontroller and pushing that view controller on current view..please see my edited question. Still it doesn't work! – Paresh Masani Jul 25 '11 at 16:21
  • by doesn't work, you mean the button doesn't show up or the favouriteButtonClicked method is not called? – Maggie Jul 25 '11 at 16:34
  • the button doesn't show up at all! I have also tried putting code in my viewcontroller's didviewload method. I am putting this favourite button(declared and initialized in basecontroller) in all my controller using [self.navigationController.view addSubview:favButton]; method and it works fine. It hasn't been working only for one of viewcontroller and the difference is it is getting pushed from parent view. The only method works is using UIBarButtonItem with initWithBarButtonSystemItem!!! I have also tried using initwithImage doesn't work!!! Not sure what's going on! – Paresh Masani Jul 26 '11 at 09:02
  • 3
    Maggie, you code works but I just need to set frame for button it seems! – Paresh Masani Jul 26 '11 at 09:28
  • Also I have edited my question for something strange happening that I don't understand. Let me know if you have any idea on that. Thank you very much. – Paresh Masani Jul 26 '11 at 09:34
  • I will let you know as soon as I get a look. – Maggie Jul 26 '11 at 10:56
  • Maggie, I am trying to use same code for MPMoviePlayerViewController but it doesn't work! I am not able to understand why. :( I have edited my question. – Paresh Masani Jul 28 '11 at 09:19
  • It should set UIButton bounds. For instance, favButton.bounds = CGRectMake(0, 0,yourImage.size.width, yourImage.size.height); – YuAn Shaolin Maculelê Lai Feb 02 '15 at 10:02
  • I posted and answer with Maggie's solution as applied to **Swift**. – CodeMonkey Jul 11 '15 at 09:21
9
UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
 [button addTarget:self action:@selector(methodAction:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 49, 30)];
 navController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

Assign it to the navigation controller and also you can set image as described in my code above.

Praveen S
  • 10,355
  • 2
  • 43
  • 69
  • There will be just one view controller. You need to modify the buttons just for that view controller. So use self.navigationController – Praveen S Jul 25 '11 at 17:31
  • I tried doing what you suggested in viewcontroller's didviewload method and also used self.navigationController.navigationItem.rightBarButtonItem to set the custom button but button doesn't appear at all! Please also see my comment response to Maggie above. Thanks. – Paresh Masani Jul 26 '11 at 09:06
  • You are correct regarding setting frame for button but I need to add button in self.navigationItem not in self.navigationcontroller.navigationitem. Thanks. – Paresh Masani Jul 26 '11 at 09:27
  • Praveen, I can't accept multiple answers it seems! You both helped me equally good but Maggie has also given me an idea on adding code in viewdidload from there I start sorting out issue! – Paresh Masani Jul 26 '11 at 10:43
  • Yup thats good, i just wanted you to develop the Stackoverflow culture. I am upvoting her answer too. – Praveen S Jul 26 '11 at 11:05
5

All right. Finally I resolved this. Thank you Maggie and Praveen. Only following code works correctly for me. Bits of from both of your answers :)

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 25, 30, 30)];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

Thanks.

Paresh Masani
  • 7,474
  • 12
  • 73
  • 139
  • Your mistake in the first place was: `viewController2.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease]` . you should have used only `viewController2.navigationItem.rightBarButtonItem` – Maggie Jul 26 '11 at 16:20
  • It doesn't work if I put that before pushing the viewcontroller2 as in my question. It works only if I do it in didviewload! – Paresh Masani Jul 27 '11 at 14:59
2

Maggie's great solution as applied to Swift:

let favButton = UIButton()
favButton.setImage(UIImage(named: "unselected"), forState: .Normal)
favButton.addTarget(self, action: "favouriteButtonClicked:", forControlEvents: .TouchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: favButton)
CodeMonkey
  • 635
  • 1
  • 7
  • 16
1

Here is function in Swift that makes use of Maggie's answer:

class func returnBarButtonItemWithImage(theImageName:String, andDownImage theDownImageName:String, andSize theSize:CGFloat, withAction theAction:Selector, andTarget theTarget:AnyObject)->UIBarButtonItem? {

    if let myImage = UIImage(named:theImageName) {

        var myButton = UIButton(frame: CGRectMake(0, 0, theSize, theSize))

        myButton.setImage(myImage, forState: UIControlState.Normal)

        myButton.addTarget(theTarget, action: theAction, forControlEvents: UIControlEvents.TouchUpInside)

        if let myDownImage = UIImage(named: theDownImageName) {

            myButton.setImage(myDownImage, forState: UIControlState.Selected)
        }

        return UIBarButtonItem(customView: myButton)
    }

    return nil
}
PJeremyMalouf
  • 613
  • 6
  • 15
1

You can also set directly from XIB by placing a roundrect button on toolbar then changing the image of that roundrect button and setting the button to custom type. Also make the background of the button as clear color

manishnath
  • 444
  • 1
  • 3
  • 12
0

UIButton *button = [[UIButton alloc] init];

[button setImage:[UIImage imageNamed:@"settings"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(RightBarMethod:)
    forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 20, 20)];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                           initWithCustomView:button];

self.navigationItem.rightBarButtonItem = barButtonItem;
Amr Angry
  • 3,711
  • 1
  • 45
  • 37