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

当前返回按钮:

<返回

需要的后退按钮:

< 其他任何东西

这些都没有奏效:

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

当前回答

有两种方法。

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

其他回答

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

// hide navigation bar title in the next controller
let backButton = UIBarButtonItem(title: "", style:.Plain, target: nil, action: nil)
navigationItem.backBarButtonItem = 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)

如果你正在为视图控制器使用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
}

}

结果将是:

如果你对任何多星建议有问题,确保你在viewDidLoad()中注册你的UITableViewCells,而不是从init()

你可以在故事板中修改NavigationItem

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

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