如何更改“背面”的标题导航栏上的按钮
目前,左栏按钮的默认值是加载当前视图的视图的标题,换句话说,按下按钮(后退按钮)时要显示的视图。
我想将按钮上显示的文本更改为其他内容。
我尝试将以下代码行放入视图控制器的 viewDidLoad 方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我应该怎么办?
谢谢。
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.
self.navigationItem.leftBarButtonItem.title = @"Log Out";
What should I do?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
我们有两个 VC 的 A 和 B。
如果你想更改 B 中的标题,请在 A
Swift 4.1 Xcode 9.4中编写此代码
We have two VC's A and B.
If you want to change title in B, write this code in A
Swift 4.1 Xcode 9.4
斯坦的回答是最好的。但它也有一个问题,当您将控制器与选项卡栏一起使用并更改控制器的标题时,您也可以更改选项卡栏的标题。所以最好的答案是仅更改 view_controller.navigationItem.title 并使用 view_controller.navigationItem函数中的.title。
答案在这里:(使用ARC并将它们添加到视图的viewDidLoad中)
在我自己使用时,我将其设为这样的函数,只需在viewDidLoad中使用一行代码即可实现该功能。
Stan's answer was the best one. But it also have a problem, when you use the controller with a Tab Bar and change the controller's title, you could change the Tab Bar's title too.So the best answer is change the view_controller.navigationItem.title only and use the view_controller.navigationItem.title in the function.
Answer is here:(With ARC and add them into view's viewDidLoad)
In myself use, I make it a function like this, just have the feature with one line code in the viewDidLoad.
如果您不仅想将“后退”按钮的文本更改为相同的文本并保持原始的左箭头形状,而且还想在用户单击“后退”按钮时执行某些操作,我建议您看看我的“CustomNavigationController"。
If you want not only to change the text of the Back button to the same text and remain the original left-arrow shape, but also to do something when user clicks the Back button, I recommend you to have a look around my "CustomNavigationController".
这应该放置在调用标题为“NewTitle”的 ViewController 的方法中。
就在push 或popViewController 语句之前。
This should be placed in the method that calls the ViewController titled "NewTitle".
Right before the push or popViewController statement.
在 ChildVC 中,这对我有用...
在 Swift 中也适用!
In ChildVC this worked for me...
Works in Swift too!
这是 backBarButtonItem 的文档:
“父” 视图控制器):
当任何其他视图控制器 B 位于导航堆栈顶部时,并且A位于其正下方,B的后退按钮将具有标题“Short”。
Here is the documentation for backBarButtonItem:
View Controller A (the "parent" view controller):
When any other view controller B is on top of the navigation stack, and A is right below it, B's back button will have the title "Short".
在使用故事板的 Xcode 4.5 中,到目前为止,当“后退”按钮的值不必动态更改时,我发现的最简单的解决方案是使用与您要访问的视图控制器的导航项关联的“后退按钮”字段。想要“后退”按钮说点别的东西。
例如,在下面的屏幕截图中,我希望我按下的视图控制器的后退按钮将“后退”作为后退按钮的标题。
当然,如果您需要后退按钮每次都说一些稍微不同的内容,那么这将不起作用......这里有所有其他解决方案。
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.
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.
我知道,这个问题很老了,但我找到了一个很好的解决方案。
从 childView 开始工作!使用 iOS 7 进行测试。
I know, the question is very old, but I found a nice solution.
Works from childView! Tested with iOS 7.
也许我过于简单化了,但从苹果的文档来看,措辞是:
上面标记为正确的解决方案设置了父控制器的默认按钮项。这是正确的答案,但我通过在将新控制器推送到 NavigationController 堆栈之前更改 UIViewController 的 self.title 属性来解决问题。
这会自动更新下一个控制器上的后退按钮标题,只要您将 self.title 设置回 viewWillAppear 中应有的内容,我就看不到此方法造成太多问题。
Maybe I'm being over simplistic but From Apple's documentation the wording is:
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.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 inviewWillAppear
I can't see this method causing too many problems.这对我来说效果更好。尝试 :
This work better for me. Try :
在 Swift/iOS8 中,以下内容对我有用:
从 Felipe 的答案移植。
In Swift/iOS8, the following worked for me:
Ported from Felipe's answer.
好的,这就是方法。如果您“第一个”有一个视图控制器,并且通过按下按钮等导航另一个“第二个”视图控制器,您需要做一些工作。
首先,您需要在“第二个”视图控制器的 ViewDidLoad 方法中创建一个 BarButtonItem,如下所示;
完成此操作后,您需要在同一个 .m 文件中编写“btnBack”操作的代码,如下所示;
就这样。
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;
After you do that, you need to write to code for "btnBack" action in the same .m file like this;
That's all.
我有一个父视图控制器,标题很长。这导致后退按钮文本渗入子视图控制器的标题中。
在尝试了一堆不同的解决方案之后,这就是我最终所做的(扩展@john.k.doe方法):
使用Xcode 7.2,Swift 2
Navigation Item
到父视图控制器场景(不是子 VC)导航项
的属性检查器
上,输入空格<
后退按钮
字段中的 /code> 字符。稍后会详细介绍这一点。< /a>
代码片段:
说明:
返回按钮属于父视图控制器。
Navigation Item
为您提供后退按钮的句柄,以便您可以在代码或 Storyboard 中设置标题。注意:
如果将
Navigation Item
Back Button
文本保留为默认空字符串,则后退按钮标题将变为“Back”。其他方法也有效,为什么使用这个?:
虽然可以覆盖子视图控制器上的后退按钮标题,但在它已经在屏幕上短暂闪烁之前对其进行处理是一个挑战。
一些方法构建了一个新的后退按钮并覆盖现有的按钮。我确信它有效,并且在某些用例中可能是必要的。但我更喜欢尽可能利用现有的 API。
对于某些情况,更改父视图控制器的标题是最快的解决方案。但是,这会更改父标题,因此您必须管理状态。
Tab Bar Controller
的情况也会变得混乱,因为标题更改会对Tab Bar Item
标题产生副作用。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):
Using Xcode 7.2, Swift 2
Navigation Item
to the Parent View Controller scene (not the child VC)Attributes Inspector
of your newNavigation Item
, type in aspace
character in theBack Button
field. More on this later.snippet:
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.Note:
If you leave the
Navigation Item
Back Button
text as the default empty string, the back button title will become "Back".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.
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 aTab Bar Controller
because title changes cause side effects with theTab Bar Item
titles.对于使用故事板的用户,只需选择父视图控制器框架(而不是持有目标视图的视图控制器框架)(确保在导航栏上单击鼠标右键,然后打开属性检查器,您将在其中找到三个表单输入。第三个“后退按钮”就是我们正在寻找的。
For those using storyboards just select the parent (not the one that is holding target view) view controller frame (be sure you click right on the Navigation bar, then open attributes inspector, where you'll find three form inputs. The third one "back button" is that we are looking for.
Swift 版本:
在你的子 ViewController 中:
Swift version:
In your child ViewController:
这是另一种方法。
在您的父视图控制器中,实现以下方法:
在您的子视图控制器中,当您想要更改标题时,这将起作用:
我永远无法让
parentViewController
属性起作用:我不知道不知道这是一个错误还是我没有正确使用它。但是,获取 viewControllers 数组中倒数第二个视图控制器指向父视图控制器,我可以使用该引用正确调用父方法。
Here's another way to do it.
In your parent view controller, implement the following method:
In your child view controller, when you want to change the title, this will work:
I was never able to get the
parentViewController
property to work:I don't know if that's a bug or I'm not using it properly. But grabbing the second-to-last view controller in the
viewControllers
array points to the parent view controller, and I can call parent methods correctly with that reference.好的。我个人讨厌所有这些选择。因此我想出了自己的。
根据我所看到的信息。看起来上一个视图控制器控制着它自己的“后退”按钮,该按钮将显示在推送的视图控制器上。
我已经为控制器上想要更改后退按钮的 navigationItem 创建了一个延迟加载方法。
我的是邀请买家控制器
邀请买家是默认设置的文本。
但后退按钮需要被邀请
这是我用来创建后退按钮的代码。
我将此代码放置在控制器的 Implementatio (.m) 文件的顶部,它会自动覆盖超级方法。
我觉得这是实现这一目标的更优雅的方式。
我将此代码放在一处,并在需要时自动填充。
无需在每次推送请求之前调用代码。
希望这有帮助
ok. I personally hated all of these options. Therefore I came up with my own.
Based on the information I have seen. It appears that the Previous view controller is in control of its own "Back" button that will be presented on the pushed view controller.
I have created a Lazy Load method for the navigationItem on the controller that wants the changed Back Button.
Mine is an Invite Buyer Controller
Invite Buyer is the text that is set by default.
but the back button needed to be Invite
Here is the code that I used to create the back button.
I placed this code in the top of the Controller's Implementatio (.m) file and it overrode the super's method automatically.
I feel this is a much more elegant way to accomplish this.
I place this code in one place, and it automatically gets populated when needed.
No need to call the code before each push request.
Hope this helps
对于斯威夫特:
For Swift:
答案如下:
在
viewDidAppear:animated
中(不在viewDidLoad
中)执行以下操作。如果您想保持后退按钮的形状,请
Here is the answer:
In
viewDidAppear:animated
(NOT inviewDidLoad
) do the followingThat if you want to keep the shape of the back button.
这里解释的解决方案都不适合我。所以我所做的就是通过以下方式从我来自的场景中删除标题:
self.title = @"";
因此,当呈现新场景时,后面的文本不会出现。
我绝对同意这根本不是一个明确的解决方案,但是有效,并且没有任何解释对我有用。
None of the solutions explained here worked for me. So what I did was remove the title from the scene where I came from in the following way:
self.title = @"";
So when new scene is presented the back text does not appear.
I absoluty agree that this is not a clear solution at all, but worked and none of the explained worked for me.
我发现最好将导航堆栈中当前视图控制器的标题更改为后退按钮所需的文本,然后再推送到下一个视图控制器。
例如
,然后在 viewDidAppear 中将标题设置回原始 VC 所需的标题。瞧!
I've found that it is best to change the title of the current view controller in the navigation stack to the desired text of the back button before pushing to the next view controller.
For instance
Then in the viewDidAppear set the title back to the desired title for the original VC. Voila!
我发现,更改后退按钮名称的最简单方法是将视图控制器标题设置为后退按钮的标题,然后将视图控制器导航项中的 titleView 替换为真实的自定义标签姓名。
像这样:
CustomViewController.m
这将使 UINavigationBar 中的标题看起来就像是原生的。使视图控制器能够具有单独的标题和后退按钮标题。
对于视图控制器 A 和 B,A 负责告知其后退按钮应如何显示,而 B 则显示。
编辑:这也保持了后退按钮的原生外观(左箭头栏按钮项目)。
I've found, that the easiest way to change the name of the back button is to set the view controllers title to the title of the back button, and then replacing the titleView in the view controllers navigation item to a custom label with it's real name.
Like this:
CustomViewController.m
This will make your title in UINavigationBar look as if it was native. Giving the view controller the ability to have seperated title and back button title.
In the case of view controller A and B, A is responsible for telling how it's back button should look, while B is displayed.
EDIT: This also maintains the back button native look (The left arrowed bar button item).
这段代码也有效。将其放在导航控制器的根控制器上:
This code works too. Put this on the root controller of the navigation controller:
我是 iOS 新手,但我将提供覆盖导航控制器类的非常简单的答案。
我有简单的覆盖推送和弹出方法并保存以前的视图控制器的标题。抱歉粘贴到 js 块中。对于如何在正常代码块中传递它有点困惑。
Im new in iOS but I will provide my very simple answer of overriding the navigation controller class.
I have simple override the push and pop methods and save the title of previous view controller. Sorry for pasting in js block. Was little confused how to past it in normal code block.
self.navigationController.navigationBar.backItem.title = @"TEXT";
在 Swift 中:
self.navigationController.navigationBar.backItem.title = @"TEXT";
And in Swift:
您可以将其放在父控制器代码中的任何位置,这样您就可以为不同的子视图提供不同的后退按钮。
you can put it whereever you like in the code in the parrent controller, which allowes you to have differenct backbuttons for different child views.
大多数解决方案都取消了后退按钮(左箭头栏按钮)的原始样式,同时添加具有所需标题的常用按钮。
所以要保持原来的风格有两种方法:
第一:使用未记录的按钮样式(110 或类似的东西),我不喜欢这样做。但如果您愿意,您可以在 stackoverflow 上找到如何执行此操作。
第二:使用 Trenskow 的想法。我喜欢它,并且我使用它时做了一些改变。
我决定以以下方式保留原始标题,而不是覆盖 - (NSString*)title (这允许我使用笔尖的标题以及推送状态下的给定标题)。
这个解决方案效果很好,而且看起来很原生。另外,如果在 viewDidLoad 方法中使用它,它会阻止执行超过 1 次。
我也尝试过 Jessedc 的解决方案,但看起来很糟糕。它会导致用户可见的标题栏从原始状态动态更改为后退按钮所需的状态并返回。
Most of solutions kills the original style of BackButton (The left arrowed bar button) while adding a usual button with desired title.
So to keep the original style there are 2 ways:
1st: To use undocumented button style (110 or something like that) which I prefer not to do. But if you want you could find how to do it here, on stackoverflow.
2nd: To use I the Trenskow's idea. I liked it and I use it a bit changed.
Instead of overriding - (NSString*)title I've decided to keep the original title in the following way (which allows me to use nib's titles as well as given title at push state btw).
This solution works good and it looks native. Also if use it in the viewDidLoad method it prevents execution more then 1 time.
Also I've tried a Jessedc's solution but it looks bad. It causes visible to user title bar change on the fly from original to BackButton's desired and back.
这对我来说是以前发布的答案的“简化”版本。
请记住将代码放入父视图控制器(例如,具有表视图或 UITableViewController 的视图)中,而不是子视图或详细视图(例如 UIViewController)中。
您可以轻松本地化后退按钮字符串,如下所示:
This works for me as a "simplified" version of previous posted answers.
Remember to put the code inside the parent view controller (e.g. the view that has your table view or UITableViewController), not the child or detail view (e.g. UIViewController).
You can easily localize the back button string like this:
根据 UINavigationBar 的文档
backItem
但是设置
backItem.backBarButtonItem
在第一次 viewWillAppear 中不起作用。设置topItem.backBarButtonItem
仅在第一次 viewWillAppear 中有效。因为navigationBar.topItem
仍然指向previousViewController.navigationItem
。在viewWillLayoutSubviews
中,topItem
和backItem
已更新。因此,在第一次viewWillAppear
后,我们应该设置backItem.backBarButtonItem
。答案:将
backBarButtonItem
设置为前一个 viewController 的navigationItem
,无论何时何地在当前 viewController(顶部 viewController)中。您可以在viewWillAppear
或viewDidLoad
中使用此代码。查看我的博文 iOS 设置导航栏返回按钮标题进行详细分析。According to document of
UINavigationBar
>backItem
But setting
backItem.backBarButtonItem
does not work in first time viewWillAppear. Setting thetopItem.backBarButtonItem
only works in first time viewWillAppear. BecausenavigationBar.topItem
is still pointing to thepreviousViewController.navigationItem
. InviewWillLayoutSubviews
, thetopItem
andbackItem
are updated. So after 1st timeviewWillAppear
, we should set thebackItem.backBarButtonItem
.ANSWER : Setting a
backBarButtonItem
to thenavigationItem
of the previous viewController no matter when and where in your current viewController (the top viewController). You can use this code inviewWillAppear
orviewDidLoad
. Check my blog post iOS Set Navigation Bar Back Button Title for detail analysis.