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.
Actually you can do this with just one trick:
Override
UINavigationBar
class and add this line of code:Then initialize your
UINavigationController
with this custom UINavigationBar class.. etc.UINavigationController * navController = [[UINavigationController alloc] initWithNavigationBarClass:[CBCNavigationBar class] toolbarClass:nil];
Hope this helps
Swift 3.1 You can do this by implementing the delegate method of UINavigationController. It'll hide the Title with back button only, we'll still get the back arrow image and default functionality.
If you are using Storyboards you can go to
Attributes Inspector
of the ViewController'sNavigation Item
(click onNavigation Bar
) and set theBack Button
property to " " (one space character). This will set the Back Button title to one space character, leaving the chevron visible. No need to mess with code.Note that this will set
Back Button
title for the Back Button that will segue to this View Controller from the one that was pushed on top of it, not for theBack Button
that will be displayed inside this Controller!When you're setting the button's title, use @" " instead of @"".
--EDIT--
Does anything change when you try other strings? I'm using the following code myself successfully:
backString is a variable that is set to @" " or @"Back", depending on if I'm on iOS 7 or a lower version.
One thing to note is that this code isn't in the controller for the page I want to customize the back button for. It's actually in the controller before it on the navigation stack.
This is using subclass
navigationController
removes the "Back".I'm using this to remove it, permanently through the app.
I was able to cobble something together using DonnaLea's answer. This is how the solution appears in my UIViewController subclass:
The problem with the original answer is that it removes the title from the controller when you pop back to it. Attempting to reset the title in viewWillDisappear is too late in the transition process; It causes the title to snap back in instead of animating nicely. However the willMoveToParentViewController happens sooner and allows for the correct behavior.
Caveat: I've only tested this with a normal UINavigationController push / pop. There might be additional unexpected behavior in other situations.