禁用在后台点击时关闭弹出框 Swift

Disable Dismiss Popover On Background Tap Swift

我有一个名为 TestViewController 的主视图控制器,它有一个按钮,当您点击该按钮时,它会打开一个弹出视图控制器。当您点击背景时,弹出窗口会消失,这是我想要禁用的。我的弹出视图控制器中有这段代码,它应该 运行 但不是 运行ning.

extension TestViewController: UIPopoverPresentationControllerDelegate {

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

        return false
    }
}

编辑:

这是我用来打开弹出窗口的代码。

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)

    }

设置委托。

popover.popoverPresentationController?.delegate = self

popoverPresentationControllerShouldDismissPopover 函数在 iOS 14.

中已弃用

对于最新版本,您应该使用以下代码

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