如何删除后退按钮的文本。
当前返回按钮:
<返回
需要的后退按钮:
< 其他任何东西
这些都没有奏效:
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
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
在我的案例中,解决方案是在移动到子视图控制器之前清除主视图控制器的导航项。如果再次显示,请重新设置
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)
斯威夫特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
解决方案检查和工作在Swift 5
下面是针对不同情况的一些解决方案:
1. 从后退按钮中删除文本
从后退按钮中删除文本的最佳解决方案是在viewDidLoad()中添加:
navigationItem.backBarButtonItem = UIBarButtonItem()
2. 在返回按钮上设置自己的文本
如果你想设置你自己的标题,可以通过设置backButton的title:
let backButton = UIBarButtonItem()
backButton.title = "My Title"
navigationItem.backBarButtonItem = backItem
3.清空所有VC上的返回按钮
如果你想在整个应用程序中创建通用样式-只有箭头返回而没有文本,为所有的视图控制器创建基础VC:
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem = UIBarButtonItem()
}
}
上面给出的解决方案可以让你在将来自定义返回按钮,如果你以后想要做一些例外,通过添加额外的变量并在特定的ViewController中重写它,f.x:
class BaseViewController: UIViewController {
var customBackButtonTitle: String?
override func viewDidLoad() {
super.viewDidLoad()
var backButton = UIBarButtonItem()
if let text = customBackButtonTitle {
backButton.title = text
}
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)