-1

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()?

Xcoder
  • 1,433
  • 3
  • 17
  • 37
  • `pushViewController` doesn't show a popover, it pushes the view onto the navigation controller. Use `present`, not `pushViewController`. – rmaddy Sep 23 '18 at 22:59
  • @rmaddy Thanks for the quick reply! I changed my code to `currentController.present(vc, animated: false, completion: nil)`, but I still seem to be getting the same results. Any ideas? – Xcoder Sep 23 '18 at 23:01
  • Typically a `popover` only is one on an iPad. It's full screen on the iPhone. (There are ways around it though.) Also, typically a "game over" popup is a `UIAlertController`. On both points, the key word is "typically". –  Sep 23 '18 at 23:49
  • https://stackoverflow.com/questions/50427668/how-to-use-popover-controller-in-iphone – matt Sep 24 '18 at 00:25

1 Answers1

0

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) {}

}
E.Coms
  • 11,065
  • 2
  • 23
  • 35
  • Thanks so much for the answer. The popover shows, but there doesn't seem to be fade in animation. What could be the cause of that? Thanks. – Xcoder Sep 30 '18 at 15:01
  • The delegate is quite simple and not complete. You need to add more details in your delegate methods. how to translate and how to present. There is a lot of work . You can learn a lot there – E.Coms Sep 30 '18 at 15:26
  • Also, is it ok if I remove the semi-colon from this line: `popoverPresentationController.delegate = self;` and also remove the {} at the end of `popoverPresentationController.sourceRect = CGRect.init(x: 200, y: 200, width: 500, height: 300)} currentController.present(vc, animated: false) {}`? – Xcoder Sep 30 '18 at 15:49
  • Thats your presentation delegate. If you remove that, your transition will not work. You need them to manager your popover transition. You may give up fancy popovers just use regular show details. It’s up to you. – E.Coms Sep 30 '18 at 15:56
  • I understand that, I'm asking if the semicolon(;) has any significance towards that line? – Xcoder Sep 30 '18 at 15:57
  • You are right. In swift that does not matter. You can delete it. – E.Coms Sep 30 '18 at 15:58