How to remove all navigationbar back button title

IosSwiftUibarbuttonitem

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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 - Ios

Work's like charm on Swift 3

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

Solution 3 - Ios

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 - Ios

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)

Solution 5 - Ios

Just need go to your Parent ViewController from where your other ViewControllers are dependent.

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(true)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)}

Solution 6 - Ios

it is simple. put a space in the title of the back button and ready. Remember that it has to be in the previous view where you want to remove the text.

enter image description here

Solution 7 - Ios

On iOS 14 is now present the backButtonDisplayMode property in UINavigationItem class. So, to remove back button title you can use

navigationItem.backButtonDisplayMode = .minimal

in the viewDidLoad func of the viewController where you want remove it.

To remove it in all navigationBar I used the swizzling technique

import UIKit

private let swizzling: (UIViewController.Type, Selector, Selector) -> Void = { forClass, originalSelector, swizzledSelector in
    if let originalMethod = class_getInstanceMethod(forClass, originalSelector), let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) {
        let didAddMethod = class_addMethod(forClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
        if didAddMethod {
            class_replaceMethod(forClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

extension UIViewController {
    
    static func swizzle() {
        let originalSelector1 = #selector(viewDidLoad)
        let swizzledSelector1 = #selector(swizzled_viewDidLoad)
        swizzling(UIViewController.self, originalSelector1, swizzledSelector1)
    }
    
    @objc open func swizzled_viewDidLoad() {
        if let _ = navigationController {
            if #available(iOS 14.0, *) {
                navigationItem.backButtonDisplayMode = .minimal
            } else {
                // Fallback on earlier versions
                navigationItem.backButtonTitle = ""
            }
        }
        swizzled_viewDidLoad()
    }
}

And in application(_:didFinishLaunchingWithOptions:) call

UIViewController.swizzle()

Solution 8 - Ios

You could create a subclass for all UIViewControllers you want this behavior for, and in the subclass's viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.backBarButtonItem = UIBarButtonItem(
        title: "", style: .plain, target: nil, action: nil)
}

This way, you can choose which controllers you want the behavior for, without duplicating code. I prefer my controllers to just say "Back", rather than the title of the previous controller, so I set that title here.

Solution 9 - Ios

You can use xcode 8 and swift 3.0

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

Solution 10 - Ios

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

    

used this line of code in swift 3.0

Solution 11 - Ios

Taking inspiration from [rordulu's][1] answer [here][2], I ended up creating a custom UINavigationController and UINavigation bar which seems to handle all cases of this tricky problem.

  1. Initialise new UINavigationController with your custom UINavigationBar:

    class CustomNavigationController: UINavigationController {

     convenience init() {
         self.init(navigationBarClass: CustomNavigationBar.self, toolbarClass: nil)
     }
    

    }

  2. Set the backItem.title property of the navigation bar to an empty string, every time the view lays itself out

    class CustomNavigationBar: UINavigationBar {

     override func layoutSubviews() {
         backItem?.title = ""
         super.layoutSubviews()
     }
    

    }

Now every time you use this navigation controller and bar combination, it will never have back button text! 

Note: this should work fine if using storyboards also, just ensure to drop the custom navigation bar component into the view [1]: https://stackoverflow.com/users/1582255/rordulu [2]: https://stackoverflow.com/a/24572572/1532838

Solution 12 - Ios

I usually add or change the back button in viewDidLoad of the UIViewController.

Something like that should work:

let leftButton = UIBarButtonItem(title: "Back", style:     UIBarButtonItemStyle.Plain, target: self, action: "closeView:")
self.navigationItem.leftBarButtonItem = leftButton

Don't forget to change and implement the function that it's called to close the view.

Even easier, just change the title:

self.navigationItem.leftBarButtonItem.title = "Back"

Solution 13 - Ios

Simple Solution :

While you are pushing 2nd controller from 1st controller, put self.navigationItem.title = "" in viewWillDisappear of 1st controller. It hides back button title from 2nd controller.

Above statment hides 1st controllers title, hence when we came back we want title for 1st controller again. For that we have add title for 1st controller in viewWillAppear method of 1st controller.

Refer following methods (of 1st controller)

    override func viewWillDisappear(_ animated: Bool) {
    self.navigationItem.title = ""
}

override func viewWillAppear(_ animated: Bool) {
    self.navigationItem.title = "Title"
}

Solution 14 - Ios

Works on Swift 5:

        self.navigationItem.backBarButtonItem?.title = ""

Please note it will be effective for the next pushed view controller not the current one on the display, that's why it's very confusing!

Also, check the storyboard and select the navigation item of the previous view controller then type something in the Back Button (Inspector).

Solution 15 - Ios

A method for iOS13.

let backButtonAppearance = UIBarButtonItemAppearance(style: .plain)
backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.clear]

let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.backButtonAppearance = backButtonAppearance

UINavigationBar.appearance().standardAppearance = navigationBarAppearance

Solution 16 - Ios

Swift 3:

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

Solution 17 - Ios

Adding a second answer here as my first only partially works. This method is less elegant in the fact that it requires calling a method in each view in the application, however it works without any side-effects.

So firstly, create a UIViewController extension class with a function to remove back button text and add a custom back button:

extension UIViewController {

func setBackButton() {
    navigationController?.navigationBar.backIndicatorImage = R.image.backArrow()
    navigationController?.navigationBar.backIndicatorTransitionMaskImage = R.image.backArrow()
    navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
}

Secondly, we can simply call out to this function in the viewDidLoad of each view controller you need it in.

Solution 18 - Ios

Swift 4.2

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

Solution 19 - Ios

Updated Answer For Swift 4.2

Working with UIAppearance is a cleaner way of solving the problem but it would cause all the UIBarButtonItem to have a clear text. An improved version of the solution could be to check if the UIBarButtonItem is contained in a UINavigationBar.

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)

Solution 20 - Ios

Just create extension of UIViewController with override function awakeFromNib() and make UIBarButtonItem with an empty title and give to navigation backBarButtonItem.

extension UIViewController {
  
  open override func awakeFromNib() {
    let backBarBtnItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    navigationItem.backBarButtonItem = backBarBtnItem
  }
  
}

Solution 21 - Ios

if you want to remove back button title when you open next screen

do this inside the function initialising and pushing a new screen:

navigationItem.backButtonTitle = ""

Full usage:

    let view = SomeView()
    let controller = UIHostingController(rootView: view)
    navigationItem.backButtonTitle = ""
    navigationController?.pushViewController(controller, animated: true)

But to customise back buttons for all navigation bars in your app you need to do this:

func setupNavBarAppearance() {
    let backButtonImage = Images.west.image.withAlignmentRectInsets(UIEdgeInsets(top: -5, left: -15, bottom: -5, right: -15))
    let backButtonAppearance = UIBarButtonItemAppearance(style: .plain)
    backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.clear]
    
    let appearance = UINavigationBarAppearance()
    appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
    appearance.backButtonAppearance = backButtonAppearance
    appearance.setBackIndicatorImage(backButtonImage, transitionMaskImage: backButtonImage)
    
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().isTranslucent = false
}

You can call it from your AppDelegate.swift

Solution 22 - Ios

    if #available(iOS 14.0, *) {
        navigationItem.backButtonDisplayMode = .minimal
    } else {
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    }

Solution 23 - Ios

Swift 4.2 & 5

Instead of playing with the navigation bar tint color which will have side effects if you are using image picker anytime later in your code.

Use below code:

extension UIViewController {
        open override func awakeFromNib() {
            navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    }  
}

Call it from your first ViewController:

self.awakeFromNib()

Solution 24 - Ios

Put the below code in any of the UIViewcontroller extension it will hide all the UIViewcontroller back text

open override func awakeFromNib() {
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    }

Solution 25 - Ios

I have a simple solution for those, who don't want to use method swizzling or duplicating a similar code in different view controllers.

To remove back button title, create a UINavigationController subclass and override pushViewController(_, animated:) method:

final class CustomNavigationController: UINavigationController {

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
    
        super.pushViewController(viewController, animated: animated)
    
        let backBarButtonItem = UIBarButtonItem()
        backBarButtonItem.title = nil
    
        viewController.navigationItem.backBarButtonItem = backBarButtonItem
    }
}

Solution 26 - Ios

I don't know why but I found problem with hiding back button title in iPhone pluses but in device without plus shows correct with

leftBarButtonItem.title = ""

So I found simple way. It is set tint color to clear in NavigationBar of NavigationViewController in autolayout. It may be problem if you use icons or text tiles with tint. But in my case I don't use it as all.

Solution 27 - Ios

Just use this:

func removeBackButton(vc:UIViewController) {
        let button = UIButton.init(type: .custom)
        button.setImage(UIImage.init(named:""), for: .normal)
        let leftBarButton = UIBarButtonItem.init(customView: button)
        vc.navigationItem.leftBarButtonItem = leftBarButton
}

So call this method in viewDidLoad:

override func viewDidLoad() {
        super.viewDidLoad()
     removeBackButton(vc:self)
}

Solution 28 - Ios

You can add this extension to UIViewController And then call this function in every viewDidLoad() like : self.updateBackButton()

extension UIViewController {
func updateBackButton(){
    if self.navigationController != nil {
        self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .done, target: self, action: nil)
    }
}}

Solution 29 - Ios

I would like to share a solution that works for me. Also, it can be adjusted base on your needs and requirements.

Note, in my case, I use a storyboard to specify CustomNavigationBar

Swift 4.2

class CustomNavigationBar: UINavigationBar {
    
    override func awakeFromNib() {
        super.awakeFromNib()
        guard let topItem = topItem else { return }
        removeBackButtonTitle(for: topItem)
    }

    override func pushItem(_ item: UINavigationItem, animated: Bool) {
        removeBackButtonTitle(for: item)
        super.pushItem(item, animated: animated)
    }

    func removeBackButtonTitle(for item: UINavigationItem) {
        item.backBarButtonItem = UIBarButtonItem()
    }
}

Solution 30 - Ios

Works for Swift 4.2

Using the line of code in AppDelegate file into didFinishLaunchingWithOptions

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)
    
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .highlighted)

Solution 31 - Ios

for swift 4,5

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

Solution 32 - Ios

class NavigationController: UINavigationController {

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        super.pushViewController(viewController, animated: animated)

        viewController.navigationItem.backButtonTitle = ""
    }
}

Solution 33 - Ios

in viewDidLoad()

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

Solution 34 - Ios

I was not satisfied with the setBackButtonTitlePositionAdjustment solution, so here is a swift alternative to hide all back button texts throughout the applicataion:

    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: barbuttonFont, NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Normal)
    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: barbuttonFont, NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Highlighted)

You would have to call these from the AppDelegate in the didFinishLaunchingWithOptions func.

Appearance must be set to the .Highlighted state too to avoid texts being show when the user taps the back button.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjansmaView Question on Stackoverflow
Solution 1 - IosNimit ParekhView Answer on Stackoverflow
Solution 2 - IosMahesh BabuView Answer on Stackoverflow
Solution 3 - IosKarthick CView Answer on Stackoverflow
Solution 4 - IosVivekView Answer on Stackoverflow
Solution 5 - IosPravin KambleView Answer on Stackoverflow
Solution 6 - Iososcar castellonView Answer on Stackoverflow
Solution 7 - IosGiorgioView Answer on Stackoverflow
Solution 8 - IosConnor NevilleView Answer on Stackoverflow
Solution 9 - IosAnit KumarView Answer on Stackoverflow
Solution 10 - IosKiran JadhavView Answer on Stackoverflow
Solution 11 - IosHarry BloomView Answer on Stackoverflow
Solution 12 - IospteofilView Answer on Stackoverflow
Solution 13 - IosMayur ShindeView Answer on Stackoverflow
Solution 14 - IosSaeed IrView Answer on Stackoverflow
Solution 15 - IosSoftDesignerView Answer on Stackoverflow
Solution 16 - IosAhmed SafadiView Answer on Stackoverflow
Solution 17 - IosHarry BloomView Answer on Stackoverflow
Solution 18 - IosAbhishek JainView Answer on Stackoverflow
Solution 19 - IosMd. Ibrahim HassanView Answer on Stackoverflow
Solution 20 - IosAshishView Answer on Stackoverflow
Solution 21 - IosPavel BogartView Answer on Stackoverflow
Solution 22 - IosSergeyView Answer on Stackoverflow
Solution 23 - IosMd RaisView Answer on Stackoverflow
Solution 24 - IoschannuView Answer on Stackoverflow
Solution 25 - IosNikita TeplyakovView Answer on Stackoverflow
Solution 26 - IosVasily AvilovView Answer on Stackoverflow
Solution 27 - Iosreza_khalafiView Answer on Stackoverflow
Solution 28 - IosTalha Ahmad KhanView Answer on Stackoverflow
Solution 29 - IosixiView Answer on Stackoverflow
Solution 30 - Iosveradiego31View Answer on Stackoverflow
Solution 31 - IosSargisView Answer on Stackoverflow
Solution 32 - IosSoufian HossamView Answer on Stackoverflow
Solution 33 - IosYuraView Answer on Stackoverflow
Solution 34 - IosJuan ArizaView Answer on Stackoverflow