如何删除后退按钮的文本。

当前返回按钮:

<返回

需要的后退按钮:

< 其他任何东西

这些都没有奏效:

self.navigationItem.backBarButtonItem?.title = "Back"
self.backItem?.title = ""
self.navigationController?.navigationBar.backItem?.title = ""
self.navigationItem.backBarButtonItem?.title = ""
self.navigationController?.navigationItem.backBarButtonItem?.title="Back"
self.navigationController?.navigationBar.backItem?.title = ""
self.navigationController?.navigationItem.backBarButtonItem?.title

当前回答

如果你正在为视图控制器使用xib文件,那么在你的视图控制器类中这样做。

class AboutUsViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    edgesForExtendedLayout = []
    setUpNavBar()
}

func setUpNavBar(){
    //For title in navigation bar
    self.navigationController?.view.backgroundColor = UIColor.white
    self.navigationController?.view.tintColor = UIColor.orange
    self.navigationItem.title = "About Us"

    //For back button in navigation bar
    let backButton = UIBarButtonItem()
    backButton.title = "Back"
    self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton
}

}

结果将是:

其他回答

斯威夫特4

在我的案例中,解决方案是在移动到子视图控制器之前清除主视图控制器的导航项。如果再次显示,请重新设置

MasterController

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

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

这就是我如何推送一个UIViewController并清除子控制器中的后栏按钮项:

MasterController

let childController = ChildController(collectionViewLayout: UICollectionViewFlowLayout())
childController.navigationItem.backBarButtonItem?.title = ""
navigationController?.pushViewController(childController, animated: true)

在呈现控制器的viewDidLoad方法中添加:

// hide navigation bar title in the next controller
let backButton = UIBarButtonItem(title: "", style:.Plain, target: nil, action: nil)
navigationItem.backBarButtonItem = backButton

下面的代码可以添加到视图控制器中,从你想要更改返回按钮文本的视图控制器中

斯威夫特5

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

例如:-

ViewController1 ViewController2

假设我们想要更新回viewcontroller2的标题,我们从viewcontroller1推送viewcontroller2。

那么您可以使用以下代码:-

let vc2 = storyboard.instantiateViewController(withIdentifier: "ViewController2")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:  "your custom back button title", style: .plain, target: self, action: nil)
self.navigationController?.pushViewController(vc2, animated: true)

你可以在故事板中修改NavigationItem

在后退按钮中添加一个空格并按Enter。

注意:在之前的VC中这样做。

你可以从接口生成器这样做:

单击上一个视图控制器的导航项

在属性检查器中,将后退按钮文本设置为您想要的任何文本。这就是它! !