Search code examples
iosuinavigationcontrolleruibarbuttonitemuinavigationitem

set background image of navigation's bar item


In my UINavigationController I have added an add button through the storyboard. My add button is the default "+" UIBarButtonItem from iOS/xcode. I would like to add a custom background image to the button. My codes is not working: the background image is not added.

-(void)addBackgroundToPlusSign
{
    UIImage *addBkg = [UIImage imageNamed:@"plus_sign"];
    addBkg = [addBkg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.navigationItem.rightBarButtonItem setBackButtonBackgroundImage:addBkg forState:UIControlStateNormal barMetrics:0];
}

I try calling the method in viewDidLoad and then in viewWillAppear. No luck.


Solution

  • Your issue is that you are setting the back button's background image on the right nav button... The right navigation button is not the back button. From the documentation:

    This modifier applies only to navigation bar back buttons and is ignored by other buttons.

    You want to use setBackgroundImage:forState:barMetrics: