我有一个正在调用的弹出窗口,我希望关闭弹出窗口的唯一方法是单击关闭按钮,而不是通过录制背景。目前我启动popover的代码是这样的:
let popover = storyboard?.instantiateViewController(withIdentifier: "PopoverVC") as! PopOverViewController
popover.modalPresentationStyle = .popover
popover.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate
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
dimView.isHidden = false
popover.dimView = self.dimView
self.present(popover, animated: false)
我在背景中有一个UIview,用于在弹出窗口时调暗背景,但是当你点击背景时,它会关闭弹出窗口。我如何保持弹出式打开?我认为popoverPresentationController?.passthroughViews = nil
应该解决这个问题,但事实并非如此。
修改
添加我的PopOverViewController类:
class PopOverViewController: UIViewController, UIPopoverPresentationControllerDelegate {
var dimView:UIView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
print ("test")
return false
}
@IBAction func closeButton(_ sender: Any) {
self.dismiss(animated: false, completion: nil)
dimView?.isHidden = true
}
}
答案 0 :(得分:2)
您需要使用演示控制器的委托。您已经分配了self
,只需实现以下委托方法。
extension SelfsType: UIPopoverPresentationControllerDelegate {
func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
/* This disables the automatic dismissal, but you can make it conditional, too. Such as if the user entered enough information, etc */
return false
}
}
其中SelfsType
是
self
类
popover.popoverPresentationController?.delegate = self as? UIPopoverPresentationControllerDelegate
您的代码不包含该内容。