How to remove text from the back button on the navigation bar in Xcode?

I install a custom arrow image on the navigation bar, adding the following code to the application delegate, it works, but now they need to completely remove the text for the back button.

UIImage * backButtonImage = [UIImage imageNamed: @"BackButtonGrey.png"]; backButtonImage = [backButtonImage stretchableImageWithLeftCapWidth: 15.0 topCapHeight: 30.0]; [[UIBarButtonItem appearance] setBackButtonBackgroundImage: backButtonImage forState: UIControlStateNormal barMetrics: UIBarMetricsDefault]; 
+7
source share
8 answers

I just did it like this, worked great for me :-)

 UIImage *backButtonImage = [UIImage imageNamed:@"navigationBarBack.png"]; UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; [backButton setImage:backButtonImage forState:UIControlStateNormal]; backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height); [backButton addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; self.navigationItem.leftBarButtonItem = backBarButtonItem; 
+5
source

Just move the text vertically until it no longer appears. This can be done when the application starts in the application delegate.

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

Usually this call is intended to adjust the vertical position of the text, which may vary depending on the font used. Here, the text moves far enough so that it no longer appears in the window of the "Back" button, and therefore it is clipped to nonexistence.

+9
source

I really don’t think that the developer should adjust the text offset well to hide it.

A cleaner solution would be to simply add a new button to the navigation controller's return button, where the title will be an empty line. You should add this to the previous call view controller in viewWillAppear (and not the current one):

 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = backButton; 
+9
source
 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-100.f, 0) forBarMetrics:UIBarMetricsDefault]; 
+6
source

Do not use an appearance proxy. Instead, for each view controller, enter this code in its viewDidLoad implementation:

 UIImage * backButtonImage = [UIImage imageNamed: @"BackButtonGrey.png"]; backButtonImage = [backButtonImage stretchableImageWithLeftCapWidth: 15.0 topCapHeight: 30.0]; UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = b; 

This will cause the next view controller to move to the navigation stack to have a back button consisting only of the image.

(However, I must indicate that stretchableImageWithLeftCapWidth:... deprecated. You must use resizableImage... )

+1
source

To set the text with the stand, you set the new stand to the current viewController before clicking or presenting a new one that will display the text of the lining:

In your current viewController (and not the new one that will display the back button):

 vc = [[MyNewViewController alloc]initWith...]; vc.title = @"New ViewController"; self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; [self.navigationController pushViewController:vc animated:YES]; 

So, if you want to remove the text, just use @"" as the title for the new lining.

To set the stand icon for the entire application, use the following code in the appDelegate class. Not every icon fits perfectly, so if you need to move it a bit, you can use "backInsets". In my example, the icon will move 2px down.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... ... [self customBackButtonIcon]; return YES; } - (void)customBackButtonIcon { UIEdgeInsets backInsets = UIEdgeInsetsMake(0, 0, -2, 0); UIImage *backImg = [[[UIImage imageNamed:@"back_button_white"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithAlignmentRectInsets:backInsets]; [[UINavigationBar appearance] setBackIndicatorImage:backImg]; [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backImg]; } 

Tested with iOS9

+1
source
 UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; [[self navigationItem] setBackBarButtonItem:newBackButton]; 
0
source

The perfect solution worldwide

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Normal) UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Highlighted) return true } 
-one
source

All Articles