0

I need to change the default (which was already declared in my AppDelegate) background for UIBarButtonItem in one ViewController.

So, In my AppDelegate I have:

UIImage *navBarItemBackground = [UIImage imageNamed:@"navbar_button_green"];
[[UIBarButtonItem appearance] setBackgroundImage:navBarItemBackground
                                            forState:UIControlStateNormal
                                               style:UIBarButtonItemStyleBordered
                                          barMetrics:UIBarMetricsDefault];

And when I need to restore the default background for the button - I implement in my viewController:

self.navigationItem.rightBarButtonItem = nil;
UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"Сохранить" style:UIBarButtonItemStyleBordered target:self action:@selector(clickOnSend:)];
[rightBarItem setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal style:UIBarButtonItemStyleBordered barMetrics:UIBarMetricsDefault];
self.navigationItem.rightBarButtonItem = rightBarItem;

But I think that it's not correct to set the empty file as the image. Is there more simple way to implement this behaviour? Thanks!

ShurupuS
  • 2,923
  • 2
  • 24
  • 44

2 Answers2

1

If you want to change only one or several UIViewController's UIBarButtonItems backgroundImage, you'd better change in the specific UIViewControllers as the latter case you use. while [[UIBarButtonItem appearance] setBackgroundImage:forState:style:barMetrics:]; will change all the UIBarButtonItems background image, which maybe not the better one.

chancyWu
  • 14,073
  • 11
  • 62
  • 81
  • Yes, the second part of the code is placed in separate viewController. But I need to left for this button the default iOS6 background and for all the rest buttons I need to change the background with the appearance. – ShurupuS Dec 16 '13 at 08:22
  • 1
    @ShurupuS then you just pass nil to the image parameter, it should work. – chancyWu Dec 16 '13 at 08:28
  • Yes - it works, just tried to use this solution - no the code looks a little bit simpler. Thanks! – ShurupuS Dec 16 '13 at 08:30
0

You should change your code to this:

UIBarButtonItem * rightBarItem = [[UIBarButtonItem alloc]
                                           initWithTitle: @"Сохранить"
                                           style: UIBarButtonItemStyleBordered
                                           target: nil action: nil];
UIImage *navBarItemBackground = [[UIImage imageNamed:@"navbar_button_green"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 6)];
[[UIBarButtonItem appearance] setBackgroundImage:navBarItemBackground
                                                forState:UIControlStateNormal
                                              barMetrics:UIBarMetricsDefault];

[self.navigationItem setBackBarButtonItem: rightBarItem];
Nikos M.
  • 13,685
  • 4
  • 47
  • 61
  • I need to left default only one barButton in one viewController - the other buttons should be changed with the appearance. – ShurupuS Dec 16 '13 at 08:21
  • Apply this code to change all butoons, and use specific code without [UIBarButtonItem appearance] to the viewController that needs the default one. – Nikos M. Dec 16 '13 at 08:24