Search code examples
iosswiftxcodepopover

Swift 3 - adaptivePresentationStyle is never called


I am trying to show a popover in an iPhone. I am following the suggestions that i found here and using the delegate "adaptivePresentationStyle", but this function is never called and the ViewController it will always presented in fullscreen mode. I have the "UIPopoverPresentationControllerDelegate" and the functions bellow:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        let identifier  = segue.destination.restorationIdentifier ?? ""
        if identifier == "NavigationSetup" {
            if let destinationNav   = segue.destination as? UINavigationController {
                let destination  = destinationNav.topViewController as! SetupTableViewController
                destination.popoverPresentationController?.delegate = self
                destination.popoverPresentationController?.backgroundColor  = UIColor.blue
                if self.myApp.isIpad{
                    destination.preferredContentSize  = CGSize(width: 600, height: 620)
                }else{
                    destination.preferredContentSize  = CGSize(width: 0.8 * self.view.frame.size.width, height: 0.8 * self.view.frame.size.height)
                }

                self.cellAnimations.fade(image: self.imageBlur, initOpacity: 0, endOpacity: 1, time: 0.3, completion: nil)
                destination.setupDismiss = {[weak self] () in
                    if let weakSelf = self{
                        weakSelf.cellAnimations.fade(image: weakSelf.imageBlur, initOpacity: 1, endOpacity: 0, time: 0.3, completion: nil)
                    }
                }

            }
        }

    }
    func adaptivePresentationStyle(for controller:UIPresentationController) -> UIModalPresentationStyle {
        print("adaptive was called")
        return .none
    }

So, what i am missing here ?


Solution

  • First, set a breakpoint to make sure this line is being called:

    destination.popoverPresentationController?.delegate = self
    

    Even better, rewrite like this, and set a breakpoint to make sure the inner line is being called:

    if let pop = destination.popoverPresentationController {
        pop.delegate = self
    }
    

    If it is, good! In that case the problem is probably that implementing the wrong delegate method. You want this:

    func adaptivePresentationStyle(for controller: UIPresentationController, 
        traitCollection: UITraitCollection) -> UIModalPresentationStyle {