【问题标题】:How to programmatically show modal in a UINavigationController如何以编程方式在 UINavigationController 中显示模式
【发布时间】:2018-06-26 21:18:57
【问题描述】:

在我的应用程序中,我有一个 UINavigationController,我从中推送和弹出 ViewControllers。在某些时候,我想以模态方式显示一个 VC(显示前面的控制器“下方”)。我可以通过在情节提要中设置一个 segue 来让它工作,但是我需要以编程方式完成它,而且我似乎找不到正确的魔法咒语来让它工作。

我看到了几个类似的问题,但他们似乎以模态方式显示 UINavigationController,而不是以模态方式显示 UINavigationController 堆栈上的 VC 之一。

(我在这里放了一个测试应用程序:https://github.com/SuperTango/ModalNavController,这就是这些代码和图像的来源)

“手动”代码可以:

@IBAction func goToVC2Tapped(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let destinationViewController = storyboard.instantiateViewController(withIdentifier: "VC2ViewController") as! VC2ViewController
    destinationViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
    self.navigationController?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
    self.navigationController?.pushViewController(destinationViewController, animated: true)
}

但它不起作用(请参阅下面 gif 中的第二个转换)。

有效的转场设置如下:

这个 gif 来自测试应用,展示了它如何与 segue 一起工作,但不是手动的。

有什么想法吗?谢谢!

【问题讨论】:

    标签: swift uinavigationcontroller


    【解决方案1】:

    要以模态方式呈现,您需要使用:

    present(destinationViewController, animated: true, completion: { })
    

    【讨论】:

    • 即使我想以模态方式呈现,我仍然希望将“VC2”推送到导航堆栈,我不想在导航堆栈之外呈现
    猜你喜欢
    • 1970-01-01
    • 2015-03-14
    • 2016-06-04
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    相关资源
    最近更新 更多