swiftuipopovercontroller

Disable Dismiss Popover On Background Tap Swift


I have a main view controller called TestViewController that has a button and when you tap the button, it opens a popover view controller. When you tap on the background, the popover gets dismissed which is what I want to disable. I have this code in my popover view controller and it should run but it's not running.

extension TestViewController: UIPopoverPresentationControllerDelegate {

    func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
        print ("TEST") //This does not show up in console

        return false
    }
}

EDIT:

This is the code that I use to open the popover.

let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC") as! PopOverViewController

        popover.modalPresentationStyle = .popover

        popover.popoverPresentationController?.sourceView = self.view
        popover.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)

        popover.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)


        popoverPresentationController?.passthroughViews = nil

        popover.dimView2 = self.dimView2

        dimView2.isHidden = false

        self.present(popover, animated: false)

    }

Solution

  • Set the delegate.

    popover.popoverPresentationController?.delegate = self