当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前回答
斯威夫特4 iOS 11.2 Xcode 9.2
TableViewController1 ---segue---> TableViewController2
你可以在TableViewController1或TableViewController2中更改返回按钮的文本。
更改TableViewController1内部的返回按钮文本:
1)在viewWillAppear():
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let myBackButton = UIBarButtonItem()
myBackButton.title = "Custom text"
navigationItem.backBarButtonItem = myBackButton
}
由于某种原因,viewDidLoad()还太早,不能将后退按钮添加到NavigationItem中。要连接两个tableviewcontroller,在storyboard控件中,从TableViewController1中的TableViewCell拖到TableViewController2的中间,然后在弹出菜单中选择Selection Segue > Show。
2)在tableView(_:didSelectRowAt:):
override func tableView(_ tableView: UITableView, didSelectRowAt: IndexPath) {
let myButton = UIBarButtonItem()
myButton.title = "Custom text"
navigationItem.backBarButtonItem = myButton
performSegue(withIdentifier: "ShowMyCustomBackButton", sender: nil)
}
要连接两个tableviewcontroller,在storyboard控件中,从TableViewController1上面的黄色小圆圈拖到TableViewController2中间,然后从弹出菜单中选择Manual Segue > Show。然后选择连接两个TableViewControllers的segue,在属性检查器中“Identifier”旁边输入“ShowMyCustomBackButton”。
3)在故事板中:
如果你只是需要静态的自定义文本返回按钮,为TableViewController1选择NavigationItem(它在故事板的目录表中有一个<图标),然后打开属性检查器,在“back button”字段中输入你的自定义文本(确保从该字段中tab出来,更改生效)。
更改TableViewController2内部的返回按钮文本:
1)在viewWillAppear():
class MySecondTableViewController: UITableViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let myBackButton = UIBarButtonItem(
title: "<Custom text",
style: .plain,
target: self,
action: #selector(goBack) //selector() needs to be paired with an @objc label on the method
)
navigationItem.leftBarButtonItem = myBackButton
}
@objc func goBack() {
navigationController?.popViewController(animated: true)
}
要连接两个tableviewcontroller,在storyboard控件中,从TableViewController1中的TableViewCell拖到TableViewController2的中间,然后在弹出菜单中选择Selection Segue > Show。
其他回答
我知道,这个问题很老了,但我找到了一个很好的解决方案。
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Custom Title";
self.navigationController.navigationBar.topItem.backBarButtonItem = barButton;
来自childView的作品!在iOS 7上测试。
下面是backBarButtonItem的文档:
时,此导航项位于顶部项的正下方 堆栈时,导航控制器派生返回按钮 此导航项中的导航栏。[…如果你想的话 为后退按钮指定自定义图像或标题,则可以指定 自定义栏按钮项目(与您的自定义标题或图像)到此 财产。”
视图控制器A(父视图控制器):
self.title = @"Really Long Title";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Short" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
当任何其他视图控制器B在导航堆栈的顶部,而A在它的正下方时,B的后退按钮的标题将是“Short”。
斯坦的回答是最好的。但它也有一个问题,当你使用带有标签栏的控制器并更改控制器的标题时,你也可以更改标签栏的标题。所以最好的答案是改变view_controller。navigationitem。只使用view_controller.navigationItem。函数中的标题。 答案在这里:(使用ARC并将它们添加到视图的viewDidLoad中)
static NSString * back_button_title=@"Back"; //or whatever u want
if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
custom_title_view.text = view_controller.navigationItem.title; // original title
custom_title_view.font = [UIFont boldSystemFontOfSize:20];
custom_title_view.backgroundColor = [UIColor clearColor];
custom_title_view.textColor = [UIColor whiteColor];
custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
custom_title_view.shadowOffset = CGSizeMake(0, -1);
[custom_title_view sizeToFit];
view_controller.navigationItem.titleView = custom_title_view;
view_controller.navigationItem.title = back_button_title;
}
在我自己的使用中,我使它成为一个这样的函数,只是在viewDidLoad中有一行代码的特性。
+ (void)makeSubViewHaveBackButton:(UIViewController*) view_controller{
static NSString * back_button_title=@"Back"; //or whatever u want
if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
custom_title_view.text = view_controller.navigationItem.title; // original title
custom_title_view.font = [UIFont boldSystemFontOfSize:20];
custom_title_view.backgroundColor = [UIColor clearColor];
custom_title_view.textColor = [UIColor whiteColor];
custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
custom_title_view.shadowOffset = CGSizeMake(0, -1);
[custom_title_view sizeToFit];
view_controller.navigationItem.titleView = custom_title_view;
view_controller.navigationItem.title = back_button_title;
}
}
这应该放在调用名为NewTitle的ViewController的方法中。 就在push或popViewController语句之前。
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:@"NewTitle"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[newBackButton release];
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(OnClick_btnBack:)];
self.navigationItem.leftBarButtonItem = btnBack;
[btnBack release];