简体   繁体   English

iOS Swift:使用“返回”文本更新导航按钮

[英]IOS Swift: Update navigation button with “Back” text

There are few similar kind of questions asked and answered by many on Stackoverflow. 在Stackoverflow上,很少有人问和回答类似的问题。 But none of solution matches with my requirement. 但是没有解决方案符合我的要求。

I am using Swift3 for IOS mobile app development and used Navigation controller to manage the navigation. 我正在使用Swift3进行IOS移动应用程序开发,并使用了导航控制器来管理导航。 I gave title to all pages using below code. 我使用以下代码为所有页面赋予了标题。

self.title = "Title"

When I move to next page, then it shows me back button with the earlier page title. 当我移至下一页时,它将显示具有上一页标题的后退按钮。 For some pages, title is long and it disturbs my header section of page. 对于某些页面,标题很长,会打扰我页面的标题部分。

Instead of page title, I want to change button text to "Back". 我想将按钮文本更改为“返回”,而不是页面标题。 Any idea how to do that? 任何想法如何做到这一点?

if you are using storyboard then you can set back button title there, click on ViewController -> Navigation Item -> Back Button and set "Back" title. 如果您正在使用情节提要,则可以在此处设置后退按钮标题,单击ViewController->导航项->后退按钮,然后设置“后退”标题。

在此处输入图片说明

alternatively you can set title in ViewWillApear and change it to "Back" in viewWillDisappear method 或者,您可以在ViewWillApear设置标题, ViewWillApearviewWillDisappear方法ViewWillApear其更改为“ Back”

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.title = "My Title"
}

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.title = "Back"
}

In your prepare function before the segue do this: 在segue之前的准备函数中执行以下操作:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Back" // Change to desired title here
    navigationItem.backBarButtonItem = backItem
}

尝试删除的标题viewWillDisapperar然后重新进入它viewWillAppear

Try this: 尝试这个:

self.navigationController?.navigationBar.topItem?.title = "Back"

It should work :) 它应该工作:)

尝试这个 :

self.navigationController!.navigationBar.topItem!.title = "Back"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM