简体   繁体   中英

Presenting a view in fullscreen not working

I am trying to present a view that is showd from the bottom to the top. This is my code

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

Despite I am using .fullScreen , the view is not presented on full screen. I tried using peformSegue to show the view with this code

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

the page is presented on fullscreen but from the left to the right and not from the bottom to the top.

The third code I tried is this one

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

here I get an error Application tried to present modally an active controller . I tried to add self.dismiss when disappearing MyPageViewController but it didn't helped.

The problem maybe is you are not presenting navigationController you are presenting detail vc, Use this

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

if problem still exists use

navigationController.modalPresentationStyle = .overCurrentContext

You could try this, worked for me:

Setup up your segue:

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

Then use the prepare for segue method:

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

Try this one:

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

I ran into this problem and was stumped. Then I realized that overriding dismiss also overrides .fullscreen

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

It's clear that you don't need this if .fullscreen is being used, but leaving it in place caused problems.

You need to change your code with below lines and then Booom work like a charm

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

Explanation: Its very simple. Its better to explain it line by line. Line 1: Declare the viewcontroller. Line 2: Add Navigation Controller (its optional. If you don't need, then you may skip navigation bar). Line 3: We have defined the modal presentation style link as overCurrentContext. and present the navigationController (it will come up with navigation bar and viewcontroller) or only the viewcontroller as per your need. In this piece of code, you will get the viewcontroller without navigation bar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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