Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController Back button doesn't hide title on iOS 11

I've updated my device to iOS 11 Beta yesterday and my app using this code in AppDelegate for hide back button title on all screen:

@implementation UINavigationItem (Customization)

/**
 Removes text from all default back buttons so only the arrow or custom image shows up.
 */
-(UIBarButtonItem *)backBarButtonItem
{
    return [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}

It's working normally on older version but when I run my app on iOS 11 Beta, the title of back button still shown. Does anyone face this problem? Is it a beta version bug of iOS or iOS 11 need another way to hide the back button title?

like image 709
Bad_Developer Avatar asked Jun 29 '17 01:06

Bad_Developer


2 Answers

What I did in iOS 11 was simply to implement the UINavigationControllerDelegate protocol for my root navigation controllers and to set the "blank" UIBarButtonItem as back button everytime a new controller will be displayed. Here is the Swift 4 version:

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
like image 55
croX Avatar answered Oct 14 '22 19:10

croX


I've been using your approach previously, but unfortunately it's not working any more. After trying all possible solutions, that's the only thing that I found working without any issues and bugs. Please note, that it seems that there's no more a universal way to fix this globally for all UIViewControllers.

  1. Call

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:self.navigationItem.backBarButtonItem.style target:nil action:nil];
    

on viewWillDisappear of the presenting controller.

  1. Call

    self.title = @"Title"
    

on viewWillAppear of the presenting controller.

Other solutions that I have tried have various issues, e.g. they are working fine but everything breaks when you swipe from left edge a bit.

like image 20
CrazyJoeLv Avatar answered Oct 14 '22 20:10

CrazyJoeLv