How to remove all navigationbar back button title

When I push a UIViewController, it has some title in back button at new UIViewController, if the title has a lot of text, It does not look good in iPhone 4s So I want to remove it.

If I add some code in prepareForSegue function, it is going to be a trouble.

Any better way to achieve this?


Solution 1:

If you want back arrow so following code put into AppDelegate file into didFinishLaunchingWithOptions method.

For Objective-C

 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

For Swift

let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)

Another option give below.

In Objective C

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

In Swift

self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)

UPDATE :

    let BarButtonItemAppearance = UIBarButtonItem.appearance()

    let attributes: [NSAttributedStringKey: Any] = [
    BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
            NSAttributedStringKey.font: UIFont.systemFont(ofSize: 0.1),
            NSAttributedStringKey.foregroundColor: UIColor.clear]

    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)

UPDATE SWIFT 4.1 :

    let attributes = [NSAttributedStringKey.font:  UIFont(name: "Helvetica-Bold", size: 0.1)!, NSAttributedStringKey.foregroundColor: UIColor.clear]

    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)

Using Offset

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000, 0), for:UIBarMetrics.default)

Solution 2:

Work's like charm on Swift 3

self.navigationController?.navigationBar.topItem?.title = " "

Solution 3:

I'm using this line of code in AppDelegate file into didFinishLaunchingWithOptions method to remove the backbutton title.

Swift 2.x

let barAppearace = UIBarButtonItem.appearance()
barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), forBarMetrics:UIBarMetrics.Default)

Swift 3.x

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)

Swift 4.x

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: UIControlState.highlighted)

Solution 4:

Just copy this code in didFinishLaunchingWithOptions launchOptions

Swift 5

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -1000.0, vertical: 0.0), for: .default)

Swift 4

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000.0, 0.0), for: .default)