Search code examples
iphoneobjective-cuibarbuttonitemappdelegateuiappearance

UIBarButtonItem appearance in App Delegate non stretchable


I need to set an image as the back button for my entire app. I tried this code in my AppDelegate.m file:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back-button.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

This code works fine with one caveat; I have an image for the back button with texture on it, so when it stretches, it looks horrible. I tried this code:

UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape
UIImage *backImage = [UIImage imageNamed:@"back-button.png"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[backButton setTitle:@"Back" forState:UIControlStateNormal];

[[UIBarButtonItem appearance] setBackBarButtonItem:backItem];

I don't get any errors, but when I try to run the app, it fails, with this reason:

reason: [_UIBarItemAppearance setBackBarButtonItem:]: unrecognized selector sent to instance

I understand that the reason why it's failing is the code that I have. What I need to know is what code I should replace it with. Can anyone help?


Solution

  • Since the appearance proxy doesn't support setBackBarButtonItem:, you'll have to call it on each instance of UIBarButtonItem in your app.