Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIBarButtonItem appearance and setBackButtonBackgroundImage

I change my back button when pushing a new viewcontroller in my navigationcontroller. But it doesnt look nice and its stretched. Also, how can I remove the "News" title in my back button? here's my code. see the image

screen shoot

and the code is

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

I want to achieve something like this http://i228.photobucket.com/albums/ee262/romano2717/photo4.png

like image 587
Diffy Avatar asked Jan 12 '12 07:01

Diffy


1 Answers

Use the following code to put the image in position.

int imageSize = 20; //REPLACE WITH YOUR IMAGE WIDTH

UIImage *barBackBtnImg = [[UIImage imageNamed:@"NavBackButton"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, imageSize, 0, 0)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:barBackBtnImg 
                                                  forState:UIControlStateNormal 
                                                barMetrics:UIBarMetricsDefault];

You can use resizableImageWithCapInsets to specify which pixels should not be stretched. If you put your image-width in there it won't stretch your image.

You don't have to put it in the .m file btw. (You could use the appDelegate.m file) just make sure it get's called before the NavigationBar is drawn.

like image 118
Tieme Avatar answered Oct 21 '22 11:10

Tieme