How to remove all navigationbar back button title

后端 未结 30 3291
后悔当初
后悔当初 2020-12-12 15:42

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

相关标签:
30条回答
  • 2020-12-12 16:07

    Work's like charm on Swift 3

    self.navigationController?.navigationBar.topItem?.title = " "
    
    0 讨论(0)
  • 2020-12-12 16:08

    for swift 4,5

    let BarButtonItemAppearance = UIBarButtonItem.appearance()
    BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
    
    0 讨论(0)
  • 2020-12-12 16:09

    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()
        }
    }
    
    0 讨论(0)
  • 2020-12-12 16:10

    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.

    0 讨论(0)
  • 2020-12-12 16:11

    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
      }
    
    }
    
    0 讨论(0)
  • 2020-12-12 16:13

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

    0 讨论(0)
提交回复
热议问题