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

当前返回按钮:

<返回

需要的后退按钮:

< 其他任何东西

这些都没有奏效:

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

当前回答

试试这个…它将工作....

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    self.title = ""
}

上面的代码将隐藏文本,只显示导航栏上的后退箭头。

其他回答

Swift 4 -在按任何视图控制器之前配置后退按钮

// if you want to remove the text
navigationItem.backBarButtonItem = UIBarButtonItem()

// if you want to modify the text to "back"
navigationItem.backBarButtonItem = UIBarButtonItem(title: "back", style: .plain, target: nil, action: nil)

这应该可以工作:

override func viewDidLoad() {
    super.viewDidLoad()

    var button = UIBarButtonItem(title: "YourTitle", style: UIBarButtonItemStyle.Bordered, target: self, action: "goBack")
    self.navigationItem.backBarButtonItem = button

}

func goBack()
{
    self.navigationController?.popViewControllerAnimated(true)
}

虽然不建议这样做,因为这实际上取代了backButton,它也删除了后退箭头和滑动手势。

back按钮属于前一个视图控制器,而不是当前显示在屏幕上的那个。 要修改返回按钮,你应该在按之前更新它,在启动segue的视图控制器上:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Something Else"
    navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
}

Swift 3,4和5:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Something Else"
    navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
}

OR

// in your viewDidLoad or viewWillAppear
navigationItem.backBarButtonItem = UIBarButtonItem(
    title: "Something Else", style: .plain, target: nil, action: nil)

斯威夫特4.2

如果你想改变导航栏后退按钮的文本,把它放在控制器的viewDidLoad中,在后退按钮显示之前,而不是在后退按钮可见的视图控制器上。

 let backButton = UIBarButtonItem()
 backButton.title = "New Back Button Text"
 self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton

如果你想改变当前导航栏标题文本,请使用下面的代码(注意,这将成为NEXT视图推到导航控制器上的默认回显文本,但这个默认回显文本可以被上面的代码覆盖)

 self.title = "Navigation Bar Title"

有两种方法。

1.在previousViewController.viewDidLoad()中

let backBarBtnItem = UIBarButtonItem()
backBarBtnItem.title = "back"
navigationItem.backBarButtonItem = backBarBtnItem

2.在currentviewcontroller。viewdidappear()中

let backBarBtnItem = UIBarButtonItem()
backBarBtnItem.title = "back"      
navigationController?.navigationBar.backItem?.backBarButtonItem = backBarBtnItem

原因:backButton来自navigationBar.backItem。backBarButtonItem,第一种方法很明显。在currentViewController.viewDidLoad()中,我们无法获得backItem的引用,因为在viewDidAppear()中,navigationBar将navigationView推入了它的堆栈。因此我们可以在currentViewController.viewDidAppear()中对backItem进行更改

更多细节,你可以看到文档:UINavigationBar