简体   繁体   中英

How to hide title near the navigation icon in navigation bar ios swift

I am developing an ios application using storyboard and swift. I have a small issue with the title near the navigation icon not hiding.I am setting the code like self.navigationController?.navigationItem.backBarButtonItem?.title = ""

But it also not worked. I have attached the screenshot. How to fix this?

在此处输入图像描述

This is pretty simple to do. But first a little of theory. The backBarButonItem does not affect the current ViewController , it affect the next in the stack of the UINavigationController . I will give you an example. If you have the ViewController A and the ViewController B , then to accomplish your task, you set the backBarButtonItem in A with a blank space in the title . Then when you go A -> B you will see only the arrow in the top left corner.

Exist two way to solve this issue, one in the storyboard and the other is in code.

  1. For the storyboard you need to set like in the next picture the Back Button title with an single space like you can see

在此处输入图像描述

  1. The second way is in code, but for this you need an extra code get the dismiss appropriately

First put this code anywhere in your project

extension UIViewController {
    @IBAction func unwind(_ sender: UIStoryboardSegue)
    {

    }
}

With that, then in A ViewController you can do then this:

class ViewControllerA: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: self, action: #selector(unwind(_:)))
    }


}

Good coding. Best regards

Add the below code in your viewDidLoad method

self.navigationController?.navigationBar.topItem?.title = ""
  • Note: This will remove back button title of current viewController and also it will clear the title of previous ViewController (where the back button is going to), so you have to set the title in viewWillAppear of previous ViewController if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM