Search code examples
iosswiftuiviewpopover

PushViewController() causes popover to be full screen


I want to push a popover on top of my view controller. Currently, this code resides in my subclass of UIView:

func presentGameOver() {
        let transition: CATransition = CATransition()
        transition.duration = 0.75
        transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        transition.type = CATransitionType.fade

        let currentController = (getCurrentViewController() as! UINavigationController).topViewController!
        currentController.navigationController!.view.layer.add(transition, forKey: nil)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "GameOverViewController") as! GameOverViewController
        vc.modalPresentationStyle = UIModalPresentationStyle.popover
        vc.highScore = highScore
        vc.score = score
        vc.FONT_H = FONT_H
        vc.delegate = self

        currentController.navigationController?.pushViewController(vc, animated: false)
    }

This is my class declaration:

class GridView: UIView, ModalHandler, UIPopoverPresentationControllerDelegate {

I have these two methods as well:

func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
    return false
}

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.none
}

In the storyboard, for the view controller I want to be a popover, I set the content size(this and this).

However, when the popover is shown, it is shown full screen. When I previously used popovers, I would present them. Does popover display not work using pushViewController()?


Solution

  • The animation is quite complicated if you have little experience. The following code may give you some clues.

        func presentGameOver() {
        let transition: CATransition = CATransition()
        transition.duration = 0.75
        transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        transition.type = CATransitionType.fade
    
        let currentController = (getCurrentViewController() as! UINavigationController).topViewController!
       currentController.navigationController!.view.layer.add(transition, forKey: nil)
    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "GameOverViewController") as! GameOverViewController
        vc.modalPresentationStyle = UIModalPresentationStyle.popover
        vc.highScore = highScore
        vc.score = score
        vc.FONT_H = FONT_H
        vc.delegate = self
        if let popoverPresentationController =  vc.popoverPresentationController{
            popoverPresentationController.delegate = self;
            popoverPresentationController.sourceView  = self
            popoverPresentationController.sourceRect  = CGRect.init(x: 200, y: 200, width: 500, height: 300)}
        currentController.present(vc, animated: false) {}
    
    }