Search code examples
iosobjective-cuiimageuibarbuttonitemuitoolbar

Tried to add custom UIBarButtonItem to UIToolBar, but item comes out messed up


I'm trying to add a custom button (shape and color) to my UIToolBar, but it comes out much differently than it should.

What button looks like:

enter image description here

What it looks like in the bar:

enter image description here

Here's the code I used to add it:

    UIImage *backButtonImage = [UIImage imageNamed:@"back-button.png"];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTapped)];
    [toolBarItems addObject:backButton];

What exactly am I doing wrong here?


Solution

  • Try using initWithCustomView: instead of the other initialization method

    So you should first create a UIButton with the custom image and selector you want and then use this piece of code

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:yourButton];
    [toolBarItems addObject:backButton];