ios 如何在 Swift 中设置后退按钮文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28471164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set back button text in Swift
提问by Onichan
How do you remove the back button text.
您如何删除后退按钮文本。
Current back button:
当前后退按钮:
< Back
<返回
Desired back button:
所需的后退按钮:
< AnythingElse
< 别的
None of these have worked:
这些都没有奏效:
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
回答by Yariv Nissim
The back button belongs to the previousview controller, not the one currently presented on screen.
To modify the back button you should update it beforepushing, on the view controller that initiated the segue:
后退按钮属于前一个视图控制器,而不是当前显示在屏幕上的那个。
要修改后退按钮,您应该在推送之前在启动 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:
斯威夫特 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)
回答by Ajinkya Patil
回答by minhazur
Back-button text is taken from parent view-controller's navigation item title. So whatever you set on previous view-controller's navigation item title, will be shown on current view controller's back button text. You can just put "" as navigation item title in parent view-controller's viewWillAppear method.
后退按钮文本取自父视图控制器的导航项标题。因此,无论您在前一个视图控制器的导航项标题上设置什么,都将显示在当前视图控制器的后退按钮文本上。您可以在父视图控制器的 viewWillAppear 方法中将 "" 作为导航项标题。
self.navigationItem.title = ""
Another way is to put
另一种方法是把
self.navigationController?.navigationBar.topItem?.title = ""
in current view controller's viewWillAppear method. This one will cause some other problem if navigation stack is too nested.
在当前视图控制器的 viewWillAppear 方法中。如果导航堆栈嵌套过多,这将导致一些其他问题。
回答by Hitesh Chauhan
You can put this 3 line of code in the ViewController
you want to change the back button title.
您可以将这 3 行代码放在ViewController
要更改后退按钮标题的位置。
In your override func viewDidLoad() {}
.
在您的override func viewDidLoad() {}
.
let backButton = UIBarButtonItem()
backButton.title = "My Back Button Title"
self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton
回答by mustaq
If you are using xib file for view controller then do this in your view controller class.
如果您将 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
}
}
The result will be:
结果将是:
回答by Francisco Romero
I do not know where you have used your methods that you put on your question but I could get the desired result if I use, on my ViewController
class (in which I want to change the back button), on viewDidLoad()
function, the following line:
我不知道您在哪里使用了您提出问题的方法,但是如果我在我的ViewController
课程(我想更改后退按钮)上viewDidLoad()
使用以下行,我可以获得所需的结果:
self.navigationController?.navigationBar.backItem?.title = "Anything Else"
The result will be:
结果将是:
Before
前
After
后
回答by Mohammad Razipour
The back button belongs to the previous view controller, not the one currently presented on screen. To modify the back button you should update it before pushing, add viewdidload :
后退按钮属于前一个视图控制器,而不是当前显示在屏幕上的那个。要修改后退按钮,您应该在推送之前更新它,添加 viewdidload :
Swift 4:
斯威夫特 4:
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
回答by xhinoda
回答by budidino
Swift 4- Configure the back button before pushing any view controllers
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)
回答by Dejan Skledar
This should work:
这应该有效:
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)
}
Although it is not recommended since this actually replaces the backButton and it also removed the back arrow and the swipe gesture.
虽然不推荐这样做,因为这实际上取代了 backButton 并且它还删除了后退箭头和滑动手势。