1

im trying to set a custom back button for my app's navigation bar. right now im using this code to do it:

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

This ends up distorting my image and making it stretched out. it also adds the text over it. How would I make it so that it just displays this image?

Here is the image: http://thai-flashcards.info/images/arrow-blue-rounded-left.jpg?1303347394

will
  • 11
  • 2

2 Answers2

2

What you're looking for is a stretchable image.

this should get you sorted out:

http://idevrecipes.com/2010/12/08/stretchable-images-and-buttons/

Edit: looking at your image I don't think that will work.

You're going to have to make a custom UIButton and set your back button to that custom button and handle the ViewController pop yourself.

You would make/set the button like this.

UIImage *buttonImage = [UIImage imageNamed:@"mybuttonimage.png"];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[doneButton addTarget:self action:@selector(goBack)forControlEvents:UIControlEventTouchUpInside];

[doneButton setImage:buttonImage forState:UIControlStateNormal];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithCustomView:doneButton];

    self.navigationItem.leftBarButtonItem = backButton;

And you need to add your method to goBack

-(void)goBack { // Go Back

    [self.navigationController popToViewController:
    [self.navigationController.viewControllers objectAtIndex:3] animated:YES];
}
Kenrik March
  • 369
  • 1
  • 5
  • After you make the custom barButton, does setting the `backBarButtonItem` of the `UINavigationItem` in the `UINavigationBar.backItem` handle this for you? – Jimmy Lee Mar 27 '13 at 23:30
  • 1
    Actually No! I don't know why Apple does not support it but: http://stackoverflow.com/questions/4964276/self-navigationitem-backbarbuttonitem-not-working-why-is-the-previous-menu-st – Kenrik March Mar 27 '13 at 23:37
2

Add in AppDelegate under didFinishLaunchingWithOptions

UIImage *backBtnIcon = [UIImage imageNamed:@"back.png"];
UIImage *backButtonImage = [backBtnIcon resizableImageWithCapInsets:UIEdgeInsetsMake(0, backBtnIcon.size.width - 1, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault ];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -30) forBarMetrics:UIBarMetricsDefault];
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
Puja
  • 23
  • 4