UIBarButtonItem setBackgroundImage not working any

2019-08-09 00:20发布

问题:

I have this appearance related code that was working fine in iOS 5:

-(void)customizeBackButton:(UIBarButtonItem*)backButtonItem {;
    [backButtonItem setBackgroundImage:[UIImage imageNamed:@"backbuttonitem"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}

This method is called within viewDidLoad in normal UIViewController.

Backbutton is a connected IBOutlet of a UIBarButtonItem, placed in a UIToolbar, and I am correctly seeing it when debugging. The point is that I can change tint and other appearances, but not the background images.

The code is working fine in iPhone (it's a universal app), and was working fine in iPad+iOS 5.

Am I doing something wrong or is there some kind of bug about UIBarButtonItem ?

This code is used everywhere in my app, because I have replaced default UINavigationBar back button to a normal UIBarButtonItem, because I needed to call some checking action before popping the controller.

回答1:

Try using the new API [UINavigationBar backIndicatorImage:backIndicatorImage].

It's documented here.