Reputation: 537
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
Reputation: 2650
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
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
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
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.
Call
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:self.navigationItem.backBarButtonItem.style target:nil action:nil];
on viewWillDisappear
of the presenting controller.
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