What I wanted to do is to remove the text from the 'Back' button of a UIBarButtonItem
, leaving only the blue chevron on the navigation bar. Keep in mind that I'm developing for iOS 7. I've tried several methods, including, but not limited to:
This is the image method which I did not like (the image looked out of place):
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iOS7BackButton"] style:UIBarButtonItemStylePlain target:self action:@selector(goToPrevious:)];
self.navigationItem.leftBarButtonItem = barBtnItem;
Another method I tried was this, which simply did not work (nothing was displayed):
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]init];
barBtn.title=@"";
self.navigationItem.leftBarButtonItem=barBtn;
What I wanted to achieve is something like the back buttons found in the iOS 7 Music app, which only featured a single chevron.
Thanks.
This works for me to display just the 'back' chevron without any text:
Set this property in
viewDidLoad
of the View Controller presenting the navigation bar and it will do the trick.Note: I have only tested it in iOS 7, which is within scope of the question.
Sometimes it is helpful to see things in context. Here is a minimal project that hides the "back" text but still shows the arrow.
Storyboard
There is a show segue from the "Show Second View Controller" button to the second view controller.
I also added a Navigation Item to the second view controller so that it would have a title. This is optional. It does not affect the back button.
Code
FirstViewController.swift
SecondViewController.swift
Alternate method (IB only, no code)
On the storyboard select the navigation item for the first view controller (not the second). Just enter a space for the Back Button text.
I create a custom class for
UINavigationController
and apply it to all of the navigation controllers in my app. Inside this customUINavigationController
class I set theUINavigationBar
delegate toself
once the view loads.Then I implement the delegate method:
This way I simply assign my custom class to all my navigations controllers and it clears the title from all the back buttons. And just for clarity, I always set the title for all my other view controllers inside
viewWillAppear
so that the title is always updated just before the view appears (in case it is removed by tricks like this).If like me you're using a custom view instead of the
UINavigationBar
and you're stuck with the back button then you have to do a bit of work that feels a bit cludgey.It seems like if it doesn't get presented then no matter what it'll try show a title, this means it's shown then hidden before it's drawn and solves the problem.
This is better solution.
Other solution is dangerous because it's hack.