ios 如何更改导航栏上“后退”按钮的标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1449339/
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 do I change the title of the "back" button on a Navigation Bar
提问by Dale
Currently the left bar button default value is the title of the view that loaded the current one, in other words the view to be shown when the button is pressed (back button).
目前,左栏按钮默认值是加载当前视图的标题,换句话说,按下按钮(后退按钮)时要显示的视图。
I want to change the text shown on the button to something else.
我想将按钮上显示的文本更改为其他内容。
I tried putting the following line of code in the view controller's viewDidLoad method but it doesn't seem to work.
我尝试将以下代码行放在视图控制器的 viewDidLoad 方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
What should I do?
我该怎么办?
Thanks.
谢谢。
回答by Jordan
This should be placed in the method that calls the ViewController titled "NewTitle". Right before the push or popViewController statement.
这应该放在调用名为“NewTitle”的 ViewController 的方法中。就在 push 或 popViewController 语句之前。
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:@"NewTitle"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[newBackButton release];
回答by Peter Pan
In ChildVC this worked for me...
在 ChildVC 这对我有用......
self.navigationController.navigationBar.topItem.title = @"Back";
Works in Swift too!
也适用于 Swift!
self.navigationController!.navigationBar.topItem!.title = "Back"
回答by Victor Bogdan
Here is the documentation for backBarButtonItem:
这是backBarButtonItem的文档:
"When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. [...] If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead."
“当此导航项紧邻堆栈中的顶部项下方时,导航控制器从该导航项派生导航栏的后退按钮。[...] 如果要为后退按钮指定自定义图像或标题,您可以将自定义栏按钮项目(带有您的自定义标题或图像)分配给此属性。”
View Controller A(the "parent"view controller):
视图控制器A(“父”视图控制器):
self.title = @"Really Long Title";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Short" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
When any other view controller Bis on top of the navigation stack, and Ais right below it, B's back button will have the title "Short".
当任何其他视图控制器B位于导航堆栈的顶部,而A位于其正下方时,B的后退按钮将具有标题"Short"。
回答by john.k.doe
in Xcode 4.5 using storyboard, by far the easiest solution i've found when the value of the Back button doesn't have to change dynamically is to use the "Back Button" field associated with the Navigation Item of the View Controller to which you want the "Back" button to say something else.
在使用故事板的 Xcode 4.5 中,到目前为止,当返回按钮的值不必动态更改时,我发现的最简单的解决方案是使用与视图控制器的导航项关联的“返回按钮”字段。想要“返回”按钮说点别的。
e.g. in the screenshot below, i want the Back button for the view controller(s) that i push to have "Back" as the title of the Back button.
例如,在下面的屏幕截图中,我希望我推动的视图控制器的后退按钮将“后退”作为后退按钮的标题。
of course, this won't work if you need the back button to say something slightly different each time … there are all of the other solutions here for that.
当然,如果您需要后退按钮每次都说一些略有不同的内容,这将不起作用……这里有所有其他解决方案。
回答by seniorbenelli
I know, the question is very old, but I found a nice solution.
我知道,这个问题很老了,但我找到了一个很好的解决方案。
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Custom Title";
self.navigationController.navigationBar.topItem.backBarButtonItem = barButton;
Works from childView! Tested with iOS 7.
从 childView 工作!用 iOS 7 测试。
回答by Jessedc
Maybe I'm being over simplistic but From Apple's documentation the wording is:
也许我过于简单了,但从 Apple 的文档来看,措辞是:
If a custombar button item is not specified by either of the view controllers, a default back buttonis used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack.
如果任一视图控制器未指定自定义栏按钮项,则使用默认后退按钮并将其标题设置为前一个视图控制器的 title 属性的值 - 即视图控制器上一层堆栈。
The solution marked correct above sets a default button item from the parent controller. It's the right answer, but I'm solving the issue by changing self.title
property of the UIViewController right before pushing the new controller onto the NavigationController stack.
上面标记为正确的解决方案从父控制器设置了一个默认按钮项。这是正确的答案,但我正在通过self.title
在将新控制器推送到 NavigationController 堆栈之前更改UIViewController 的属性来解决问题。
This automatically updates the back button's title on the next controller, and as long as you set self.title
back to what it should be in viewWillAppear
I can't see this method causing too many problems.
这会自动更新下一个控制器上的后退按钮的标题,只要您设置self.title
回它应该在的位置,viewWillAppear
我就看不到这种方法会导致太多问题。
回答by Duncan Smith
In Swift/iOS8, the following worked for me:
在 Swift/iOS8 中,以下对我有用:
let backButton = UIBarButtonItem(
title: "Back Button Text",
style: UIBarButtonItemStyle.Bordered,
target: nil,
action: nil
);
self.navigationController.navigationBar.topItem.backBarButtonItem = backButton;
Ported from Felipe's answer.
来自 Felipe 的回答。
回答by Felipe FMMobile
This work better for me. Try :
这对我来说更好。尝试 :
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
回答by Robert Chen
I had a parent view controller with a really long title. This resulted in the back button text bleeding into the title of the child view controller.
我有一个标题很长的父视图控制器。这导致后退按钮文本渗入子视图控制器的标题。
After trying a bunch of different solutions, this is what I ended up doing (expanding on the @john.k.doe approach):
在尝试了一系列不同的解决方案后,这就是我最终做的(扩展 @john.k.doe 方法):
Using Xcode 7.2, Swift 2
使用 Xcode 7.2、Swift 2
- In the Storyboard, add a
Navigation Item
to the ParentView Controller scene (not the child VC)
- 在 Storyboard 中,
Navigation Item
在ParentView Controller 场景中添加一个(不是子 VC)
- On the
Attributes Inspector
of your newNavigation Item
, type in aspace
character in theBack Button
field. More on this later.
- 在
Attributes Inspector
新的 上Navigation Item
,space
在Back Button
字段中输入一个字符。稍后会详细介绍。
- In the Parentview controller, add the following code:
- 在父视图控制器中,添加以下代码:
snippet:
片段:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
switch segue.destinationViewController {
case is ChildViewController:
navigationItem.backBarButtonItem?.title = ""
default:
navigationItem.backBarButtonItem?.title = "Full Parent Title"
}
}
Explanation:
解释:
The back button sort of belongs to the parent view controller. The Navigation Item
gives you a handle to the back button, so you can set the title in code or in the Storyboard.
后退按钮属于父视图控制器。该Navigation Item
给你一个句柄返回按钮,这样你就可以在代码中或在故事板设置标题。
Note:
笔记:
If you leave the Navigation Item
Back Button
text as the default empty string, the back button title will become "Back".
如果您将Navigation Item
Back Button
文本保留为默认的空字符串,则后退按钮标题将变为“后退”。
Other approaches work, why use this one?:
其他方法有效,为什么要使用这个?:
While it's possible to override the back button title on the child view controller, it was a challenge getting a handle to it until it had already flashed briefly on the screen.
虽然可以覆盖子视图控制器上的后退按钮标题,但在它已经短暂地在屏幕上闪烁之前获得它的句柄是一个挑战。
Some of the approaches construct a new back button and override the existing one. I'm sure it works, and probably necessary in some use cases. But I prefer to leverage existing APIs when possible.
一些方法构造一个新的后退按钮并覆盖现有的按钮。我确信它有效,并且在某些用例中可能是必要的。但我更喜欢尽可能利用现有的 API。
Changing the title
of the parent view controller is the quickest solution for some situations. However, this changes the parent title so you have to manage state. Things also get messy with a Tab Bar Controller
because title changes cause side effects with the Tab Bar Item
titles.
在title
某些情况下,更改父视图控制器的 是最快的解决方案。但是,这会更改父标题,因此您必须管理状态。事情也会变得混乱,Tab Bar Controller
因为标题更改会导致标题产生副作用Tab Bar Item
。
回答by EEE
Ok, here is the way. If you have a view controller "first" and you navigate another view controller "second" by pushing a button or etc. you need to do some work. First you need to create a BarButtonItem in "second" view controller's ViewDidLoad method like this;
好的,这是方法。如果您有一个“第一个”视图控制器,并且您通过按下按钮等来导航另一个“第二个”视图控制器,您需要做一些工作。首先,您需要像这样在“第二个”视图控制器的 ViewDidLoad 方法中创建一个 BarButtonItem;
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(OnClick_btnBack:)];
self.navigationItem.leftBarButtonItem = btnBack;
[btnBack release];
After you do that, you need to write to code for "btnBack" action in the same .m file like this;
完成此操作后,您需要在同一个 .m 文件中编写“btnBack”操作的代码,如下所示;
-(IBAction)OnClick_btnBack:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
//[self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];
}
That's all.
就这样。