Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing the BackBarButtonItem

I'm trying to customize the backBarButtonItem with a custom image (the text 'back' is included in that image), here is the current result:

http://i.imgur.com/rFxFt.png

Does anyone know why this might be happening?

Here is my code on viewDidLoad (actually runs both on the parent controller and then again on the new controller that has the back button)

UIImage *backButtonImage = [UIImage imageNamed:@"Graphics/Shared/navigation_back_button.png"];


UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
                               initWithImage:backButtonImage
                               style:UIBarButtonItemStylePlain 
                               target:nil 
                               action:nil];


self.navigationItem.backBarButtonItem = backButton;

Edit: I'm using iOS 5 by the way! Maybe appearance proxy is usable but so far when I tried to use it for the back button stuff (in appDelegate) the app simple crashes.

like image 410
Dom Chapman Avatar asked Jun 06 '12 00:06

Dom Chapman


People also ask

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.


1 Answers

Okay I solved the issue, using a bit of a rough trick, but at least it works. If anyone does come up with a more standard solution though, please let me know!

Here's my code:

UIImage *backButtonImage = [[UIImage imageNamed:@"Graphics/Shared/navigation_back_button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, backButtonImage.size.height*2) forBarMetrics:UIBarMetricsDefault];

This is in my appdelegate's method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

To stop it stretching when using it as a background, I used the idea from iOS 5: How to implement a custom (image) back button

But then tweaked it so rather than having to set-

self.title = @" ";

On every view load (and that might also mess with the nav bar title itself)

I just set the offset for the 'back' button text to twice the image height, so you'll never see it.

Why go to all this trouble, rather than use a left button item with it's own method to pop the view controller?

The main reason is that you lose the standard back button sliding animation for changing views. Also, this means I don't need to use a custom button or write a custom method for going back. It simply just works.

Hope this solves someone else's problem too, I know I was stuck on it for a good 3 hours!

like image 96
Dom Chapman Avatar answered Oct 13 '22 01:10

Dom Chapman