禁用关闭背景Tap Pop Swift

问题描述:

我有一个名为TestViewController的主视图控制器,它有一个按钮,当你点击按钮时,它会打开一个弹出视图控制器。当你点击背景时,popover被取消,这是我想禁用。我有我的popover视图控制器中的代码,它应该运行,但它不运行。禁用关闭背景Tap Pop Swift

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) 

    } 
+0

您是否设置了委托?发布你的代码 – Bilal

+0

@Bilal我更新了我的问题。 – Martheli

+0

@Bilal你的权利,我忘了添加'popover.popoverPresentationController?.delegate = self? UIPopoverPresentationControllerDelegate'。如果你创建这个答案,我会为你投票。 – Martheli

设置委托。

popover.popoverPresentationController?.delegate = self