Bad_Developer
Bad_Developer

Reputation: 537

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?

Upvotes: 12

Views: 3617

Answers (4)

MANISH PATHAK
MANISH PATHAK

Reputation: 2650

FOR SWIFT 3+

Use below code to support iOS 9 to 11

     if #available(iOS 11, *) {
            UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
            UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .highlighted)
     } else {
            UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0,
-60), for:UIBarMetrics.default)
        }

Upvotes: 0

croX
croX

Reputation: 1725

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)
}

Upvotes: 2

AFraser
AFraser

Reputation: 1006

Setting the bar button title text color to clear is the cleanest way I've found to apply for all screens

 UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
 UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .highlighted)

Upvotes: 0

CrazyJoeLv
CrazyJoeLv

Reputation: 570

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.

Upvotes: 6

Related Questions