Meeya
Meeya

Reputation: 53

Image on UIBarButtonItem

In my iPhone application, I'm adding a toolbar on UINavigation controller as navigation bar do not take more then two bar buttons on it. So far .. I have successfully added the UIToolBar with three BarButtonItems on it. However I am not able to display an image on BarButtonItems.

Can any one tell me .. how to add image on UIToolBar BarButtonItems? Where ToolBar is already added on nav controller.

Upvotes: 0

Views: 1022

Answers (2)

   button = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:image]];
button.action = @selector(myaction);
button.target = self;

Try this.

Upvotes: 0

kgutteridge
kgutteridge

Reputation: 8991

Set a custom view for the UIBarButtonItem, something like this

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIImage *yourImage = [UIImage imageNamed:@"yourImage.png"];
    [button setImage:yourImage forState:UIControlStateNormal];
    [button addTarget:self action:@selector(yourButtonAction) forControlEvents:UIControlEventTouchUpInside];

    [barButton setCustomView:button];
    self.navigationItem.rightBarButtonItem = barButton;
    [barButton release];

Upvotes: 1

Related Questions