【问题标题】:Presenting a view in fullscreen not working全屏显示视图不起作用
【发布时间】:2019-10-11 11:14:36
【问题描述】:

我正在尝试呈现从底部到顶部显示的视图。这是我的代码

let myPageViewController = storyboard?.instantiateViewController(withIdentifier: "myPageViewControllerID") as! MyPageViewController
myPageViewController.modalPresentationStyle = .fullScreen
let navController = UINavigationController(rootViewController: myPageViewController)
navigationController?.present(navController, animated: true, completion: nil)

尽管我使用的是.fullScreen,但视图并未全屏显示。 我尝试使用 peformSegue 显示此代码的视图

self.performSegue(withIdentifier: "myPageViewSegue", sender: nil)

页面全屏显示,但从左到右,而不是从下到上。

我尝试的第三个代码是这个

let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .fullScreen
present(detailVC, animated: true)

在这里我收到一个错误Application tried to present modally an active controller。我试图在 MyPageViewController 消失时添加 self.dismiss 但它没有帮助。

【问题讨论】:

    标签: ios swift uiviewcontroller


    【解决方案1】:

    问题可能是你没有展示navigationController 你展示的是详细的vc,使用这个

    'let detailVC = MyPageViewController()
    let navigationController = UINavigationController(rootViewController: detailVC)
    navigationController.modalPresentationStyle = .fullScreen
    present(navigationController, animated: true)'
    

    如果问题仍然存在,请使用

    navigationController.modalPresentationStyle = .overCurrentContext
    

    【讨论】:

    • 如何确保这一点永远不会被忽视?
    【解决方案2】:

    你可以试试这个,对我有用:

    设置你的转场:

    performSegue(withIdentifier: "myPageViewSegue", sender: self)
    

    然后使用prepare for segue方法:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destVC = segue.destination as! MyPageViewSegue
        destVC.modalPresentationStyle = .fullScreen 
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      let detailVC = MyPageViewController()
      detailVC.modalPresentationStyle = .fullScreen
      present(detailVC, animated: true)
      

      【讨论】:

        【解决方案4】:

        我遇到了这个问题,被难住了。然后我意识到覆盖dismiss也会覆盖.fullscreen

        // This breaks .fullscreen
        override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
          super.dismiss(animated: flag, completion: completion)
          // do something
        }
        

        很明显,如果正在使用 .fullscreen,则您不需要这个,但将其留在原处会导致问题。

        【讨论】:

          【解决方案5】:
          let detailVC = MyPageViewController()
          let navigationController = UINavigationController(rootViewController: detailVC)
          navigationController.modalPresentationStyle = .overCurrentContext
          present(detailVC, animated: true, completion: nil)
          

          说明: 它非常简单。最好逐行解释。第 1 行:声明视图控制器。第2行:添加导航控制器(它是可选的。如果你不需要,那么你可以跳过导航栏)。第 3 行:我们已将模态表示样式链接定义为 overCurrentContext。并根据您的需要显示导航控制器(它将提供导航栏和视图控制器)或仅显示视图控制器。在这段代码中,您将获得没有导航栏的视图控制器。

          【讨论】:

          • 您能否在您的答案中添加一个关于它的作用以及它是如何作用的解释?谢谢。
          • 它非常简单。最好逐行解释。第 1 行:声明视图控制器。第2行:添加导航控制器(它是可选的。如果你不需要,那么你可以跳过导航栏)。第 3 行:我们将模态表示样式 link 定义为 overCurrentContext。并根据您的需要显示导航控制器(它将提供导航栏和视图控制器)或仅显示视图控制器。在这段代码中,您将获得没有导航栏的视图控制器。
          • 将其添加到您的答案中
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多