Martheli
Martheli

Reputation: 981

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)

    }

Upvotes: 4

Views: 3115

Answers (2)

Aqeel Ahmad
Aqeel Ahmad

Reputation: 757

popoverPresentationControllerShouldDismissPopover function is deprecated in iOS 14.

For latest version you should use following code

extension TestViewController: UIPopoverPresentationControllerDelegate {
        func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
             return false
        }
    }

Upvotes: 3

Bilal
Bilal

Reputation: 19156

Set the delegate.

popover.popoverPresentationController?.delegate = self

Upvotes: 5

Related Questions