如何更改后退按钮上的文本
默认情况下,后退按钮使用视图控制器的标题作为其上的文本。 我可以在不更改视图控制器标题的情况下更改后退按钮上的文本吗? 我需要这个,因为我有一个视图控制器,其标题太长而无法显示,在这种情况下,我想仅显示“后退”作为后退按钮的标题。
我尝试了以下方法,但没有成功:
self.navigationItem.leftBarButtonItem.title = @"Back";
谢谢。
By default the back button uses as a text on it a title of a viewcontroller.
Can I change text on the back button without changing a title of a view controller?
I need this because I have a view controller which title is too long to display and in this case I would like to display just "Back" as a caption for back button.
I tried the following which didn't work:
self.navigationItem.leftBarButtonItem.title = @"Back";
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
尝试
我发现通过查看 backBarButtonItem 文档在 Apple 文档中的 UINavigationItem。
Try
I found that by looking at the backBarButtonItem docs in Apple's docs for UINavigationItem.
当我弄清楚将其应用到哪个控制器时,Marc W 的方法效果非常好:重新命名的控制器,而不是顶部的控制器。 因此,如果这是导航堆栈:
...并且您希望在 ControllerB 位于顶部 时显示的后退按钮中为 ControllerA 提供较短的标题,则可以将属性更改应用于 ControllerA 。
因此,它更多地在 self.title 的上下文中,而不是像其他左/右栏按钮设置器。
Marc W's approach worked great once I figured out which controller to apply it to: the one being re-titled, not the one on top. So if this is the navigation stack:
...and you want to give a shorter title for ControllerA in the back-button displayed when ControllerB is on top, you apply the property change to ControllerA.
So it's more in the context of
self.title
, not like the other left/right-bar-button setters.您可以在故事板中完成它。 找到要返回的视图控制器(具有长标题的视图控制器),选择其导航项,然后打开属性检查器 (Alt+Cmd+4),插入自定义后退按钮标题。
You can do it in the storyboard. Find the view controller you want to go back to (the one with the long title), select it's Navigation Item, and open the Attributes Inspector (Alt+Cmd+4), insert the custom Back Button title.
谢谢马可...这帮助了我...
这就是我所做的。
如果您使用 tableView 导航到不同的视图...请将代码:
在第一个控制器的
didSelectRowAtIndexPath
方法中...控制器 A。当您导航到控制器 B 时,按钮将有标题“返回”。
Thanks Marco... that helped me...
Here is what i did.
If you are using a tableView to navigate to different views... put the code:
In your
didSelectRowAtIndexPath
method... of the first Controller... Controller A.When you navigate to Controller B the button will have the title "Back".
后退按钮从父视图控制器的标题中提取文本。
在父视图控制器(点击后退按钮时出现的视图控制器)中,将其自己的标题设置为所需的文本返回按钮。
例如,假设我们有一个 RootViewController 类。 当我们单击表格视图中的单元格时,我们会推送一个 SecondViewController 实例。 我们希望 SecondViewController 实例的后退按钮显示为“Home”。
在RootViewController.m的
viewDidLoad
方法中:在SecondViewController.m的
viewDidLoad
方法中:如果你想要你的返回按钮读取“返回”,将父视图控制器的标题设置为
@“返回”
;The back button pulls its text from the title of the parent view controller.
In the parent view controller (the view controller that appears when you tap the back button), set its own title as the desired text on the back button.
For example, let's say we have a
RootViewController
class. When we click a cell in its table view, we push an instance ofSecondViewController
. We want the back button of theSecondViewController
instance to read, "Home."in the
viewDidLoad
method of RootViewController.m:in the
viewDidLoad
method of SecondViewController.m:If you want your back button to read, "Back," set the title of the parent view controller to
@"Back"
;这对我来说效果更好。 尝试 :
This work better for me. Try :
如果您使用 Storyboard:
返回到
就是这样,享受...
If you are using storyboard:
return to
That is it, enjoy...
在 MonoTouch 中,以下工作(在父控制器的 ViewDidLoad 中):
And in MonoTouch the following works (in ViewDidLoad of the parent controller):
这对我有用。
This worked for me.
在父视图控制器中,在视图加载时设置后退按钮:
请注意,在最新的 iOS 版本中,我们不需要在末尾包含
autorelease
。希望这可以帮助!
In your parent view controller, set the back button when view loads:
Notice that we don't need to include
autorelease
at the end with the latest iOS version.Hope this helps!
[self.navigationController.navigationBar.backItem setTitle:@"back"];
它对我有用。 您可以将“返回”替换为其他内容。
[self.navigationController.navigationBar.backItem setTitle:@"back"];
It works for me. You can replace "back" with something else.
如果你不想拥有头衔,这个对我有用!
This one worked for me if you don't want to have a title!
我终于知道为什么这些答案一开始对我不起作用。 我在故事板中设置了标题。 当我在代码上设置标题时。 有用!
I finally knew why these answers did not work for me at first. I set the title in storyboard. When i set the title on code. it works!
我的解决方案是在视图控制器被推送到导航堆栈时设置标题,并在推送 vc 关闭之前使用委托方法重置它:
因此,当我推送其他视图控制器时,我将标题更改放在调用视图控制器中,例如:
并在委托中回调函数(我在 viewWillDissapear 中调用):
My solution was to set title when the view controller is pushed to navigation stack and reset it by use of delegate method before pushed vc closes:
So I put the title change in calling view controller when I push the other view controller like:
and in delegate callback function (which I invoke in viewWillDissapear):
如果您不仅想更改“后退”按钮的文本并保持原始的左箭头形状,而且还想在用户单击“后退”按钮时执行某些操作,我建议您看看我的“CustomNavigationController"。
If you want not only to change the text of the Back button 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".
//您可以通过在上一个视图控制器中设置标题来实现此目的,如下所示
//You can achieve this by setting the title in the previous view controller as shown below
要更改后退按钮标题,请参阅下面的代码
For to change back button title, refer below code
在 Swift5 中,backBarButtom 无法编辑。 因此,我们需要先隐藏backBarButtom,然后使用自定义的leftbarButtom来替换backBarButtom。 这是详细的解决方案:https://stackoverflow.com/a/63868300/13939003
In Swift5, the backBarButtom cannot be edited. Hence, we need to hide the backBarButtom first, then use a customised leftbarButtom to replace backBarButtom. Here is the detailed solution: https://stackoverflow.com/a/63868300/13939003
这对我有用:
This worked for me: