setBackButtonBackgroundImage without title IOS5
Asked Answered
P

6

8

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 ?

Petuntse answered 11/10, 2012 at 1:12 Comment(1)
To be clear, are you just trying to make a custom button that looks like the back button with no title in it?Delocalize
S
9

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)];
Sherlock answered 17/11, 2012 at 9:22 Comment(5)
It still stretches the button image.Vacuva
Thanks. This worked for me. You forgot to mention that we need to put this in the app delegate.Palestra
@Palestra technically speaking, you have to add this code before any for your NavBars are displayed, but yeah, the appDelegate is where I tend to put itSherlock
Hacking with setBackButtonTitlePositionAdjustment in this issue is not a good workaround, I think. Because you never know when the screen size will become larger than 400 points(or any value you set in the x position of UIOffsetMake). For example, iPhone6 and iPhone6+ breaks this hack: you will get the 'back' text on the previous page in a moment after you tap the back button.Surat
@RalphZhouYuan if 400 is the sticky point, make it 4000. It is labeled as a hack, not a workaround, and there is no such thing as a good hack in the first place.Sherlock
H
4

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];
Holm answered 27/10, 2012 at 22:58 Comment(1)
@Petuntse If my answer was suitable for your needs, can you please accept? Thank you.Holm
G
0
UIImage *image1 = [UIImage imageNamed:@"image.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:image1 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Above code use for set bacground image in Back button.

Gelsenkirchen answered 30/10, 2012 at 7:1 Comment(1)
This doesn't address the fact that the button should have no title.Watermark
S
0

as i understand, you are asking how to change the back button's title.

here is how i always do it

self.navigationItem.backBarButtonItem.title=@"My Title";

i guess you can set it as an empty by not giving any title

self.navigationItem.backBarButtonItem.title=@"";

make sure to call this function before you push the new view.

Shinn answered 31/10, 2012 at 11:0 Comment(0)
A
0

i have not tried it, but i think it should work....Put this into you viewdidLoad, i hope its what you are looking for.

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" 
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
Arietta answered 2/11, 2012 at 11:10 Comment(0)
C
-4

Just add this line after the code you have already written :

[[UIBarButtonItem appearance] setTitle:@""];

Hope it works.

Chopping answered 30/10, 2012 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.