源码

使导航栏显示所需标题

有3个视图控制器/uff1aVC1 - > VC3 - > VC2。 从VC3转换到VC2我用下一个代码制作/uff1a

let storyboard = UIStoryboard(name: "VCs", bundle: nil)
let myVC2 = storyboard.instantiateViewController(withIdentifier: "VC2") as? VC2
myVC2?.name = "Test"
self.navigationController?.pushViewController(myVC2!, animated: true)

在VC2的viewDidLoad/uff08/uff09中/uff0c我从viewControllers堆栈中删除VC3/uff0c只留下VC1/uff1a

var navigationVCs = self.navigationController?.viewControllers
navigationVCs!.remove(at: 1)
self.navigationController?.viewControllers = navigationVCs!

一切顺利。 但我有一个视觉/u201c问题/u201d。 当视图控制器从VC4移动到VC3/uff0c并且VC3视图开始加载时/uff0cVC3的NavigationBar后退按钮首先显示VC4的标题/uff0c然后才切换到VC1标题。 我如何解决这个问题并强制VC2显示VC1的后退按钮标题呢/uff1f 

答案是/uff1a

我可以通过这种方式将VC1的后退按钮标题传递给VC3中的VC2/uff1a

if let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
            if let previousVC = navigationController.viewControllers.first {
                let backItem = UIBarButtonItem()
                let title = previousVC.navigationItem.backBarButtonItem?.title
                backItem.title = title
                navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
            }
        }

在那之前

self.navigationController?.pushViewController(myVC2!, animated: true)

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/shidaohanglanxianshisuoxubiaoti.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:8 月 25, 2019 at 09:32 下午

热评文章