Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i remove text from back button on navbar within Xcode?

I set an arrow custom image to navigation bar by adding the following code in app delegate, it works but now im looking to remove the text completely for back button.

UIImage * backButtonImage = [UIImage imageNamed: @"BackButtonGrey.png"];
backButtonImage = [backButtonImage stretchableImageWithLeftCapWidth: 15.0 topCapHeight: 30.0];
[[UIBarButtonItem appearance]  setBackButtonBackgroundImage: backButtonImage  forState: UIControlStateNormal  barMetrics: UIBarMetricsDefault];
like image 427
ASH Avatar asked May 11 '13 20:05

ASH


People also ask

What is the back button on the navigation item?

The back button sort of belongs to the parent view controller. The Navigation Item gives you a handle to the back button, so you can set the title in code or in the Storyboard.

What is the default value for the left bar button text?

Bookmark this question. Show activity on this post. Currently the left bar button default value is the title of the view that loaded the current one, in other words the view to be shown when the button is pressed (back button). I want to change the text shown on the button to something else.

How do I get the back button on a view controller?

For those using storyboards just select the parent (not the one that is holding target view) view controller frame (be sure you click right on the Navigation bar, then open attributes inspector, where you'll find three form inputs. The third one "back button" is that we are looking for.

How to set back button title in code?

We have two way to set back button title in code, navigationItem.backBarButtonItem and navigationItem.backButtonTitle. In view controller A, create a UIBarButtonItem with any title you want and set it to navigationItem.backBarButtonItem. This will set a back button title to "You back button title here".


2 Answers

Simply move the text vertically so far that it no longer appears. This can be done at app launch in your app delegate.

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

Normally this call is for tweaking the vertical text position which can vary depending on the font used. Here the text is moved far enough that it's no longer inside the Back button view, and so is clipped into non-existence.

like image 122
Graham Perks Avatar answered Nov 03 '22 00:11

Graham Perks


I really don't think is a good practice for a developer to adjust the offset of the text in order to hide it.

A cleaner solution would be to just add to the navigation controller back button a new button where the title is an empty string. You should add this in the previous calling view controller in viewWillAppear (not the current one):

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
like image 26
CarmenA Avatar answered Nov 03 '22 00:11

CarmenA