Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an image to UIBarButtonItem?

I created a custom button and applied it to the UIbarbuttonitem. There is no error but nothing is shown on the navigation bar:( This is my code-

    //create a custom button
    UIImage *image = [UIImage imageNamed:@"TESTButton.png"];
    UIButton *myCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myCustomButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );    
    [myCustomButton setImage:image forState:UIControlStateNormal];
    [myCustomButton addTarget:nil action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];


    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myCustomButton];
    self.navigationItem.leftBarButtonItem = button;
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

    [button release];
    [myCustomButton release];
    [image release];
    [navID release];

Anybody who can fix my code? :)

like image 521
Leanne Avatar asked Dec 22 '22 11:12

Leanne


1 Answers

From the docs:

initWithImage:style:target:action:

Try that.

like image 199
DexterW Avatar answered Jan 08 '23 02:01

DexterW