1
votes

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

2

2 Answers

2
votes

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];
}
2
votes

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];