Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setBackButtonBackgroundImage without title IOS5

I'm trying to get a back button without a title but I can't make it work. I am really new in objective-c...

UIImage *backButtonImage = [[UIImage imageNamed:@"back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 30, 50)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage
                                                  forState:UIControlStateNormal
                                                barMetrics:UIBarMetricsDefault];

With that code I have my back button but also the title of the previous page.

I found some working examples using the class UIViewController but in my case the code is in the appDelegate.m file.

Any idea how I can make it work ?

like image 455
user1736571 Avatar asked Oct 11 '12 01:10

user1736571


2 Answers

A very easy hack that I adapted which uses the iOS 5 appearance proxy is the following (needless to say, the benefit of the proxy is global change to all nav bars):

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0)
                                                     forBarMetrics:UIBarMetricsDefault];

This method is available in iOS 5 SDK, so no worries there.


EDIT

In order to avoid the image stretching, use the following:

UIImage* image = [UIImage imageNamed:@"back"];
return [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)];
like image 103
Mazyod Avatar answered Nov 09 '22 21:11

Mazyod


From what I can tell, you're setting the appearance proxy correctly. The issue here is setting a new title for your back button.

To do so, create a custom button with your behaviour, and use that as your new back button. Set this before you push or pop said view controller, such as in init

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@""
                                                               style:UIBarButtonItemStyleBordered
                                                              target:nil
                                                              action:nil];

[self.navigationItem setBackBarButtonItem: newBackButton];
like image 42
WDUK Avatar answered Nov 09 '22 20:11

WDUK