2017-07-19 78 views
0

我有一个正在调用的弹出窗口,我想要关闭弹出窗口的唯一方法是通过单击关闭按钮,而不是通过背景上的磁带。目前,我的代码启动酥料饼是这样的:Swift 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当酥料饼的出现,但是当你在后台挖掘,它将关闭酥料饼。我怎样才能让popover保持开放状态?我认为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

我们可以看到你的PopOverViewController类吗? –

+0

我将它添加到问题中。 – Martheli

回答

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 

你的代码的上下文中不包括。

+0

我试着向我的popover视图控制器添加'func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController:UIPopoverPresentationController) - > Bool return false}'并且它不起作用。点击背景仍会关闭弹出窗口。 – Martheli

+0

委托方法实际上是否被调用? (放置一个断点或打印语句以确保其被调用) –

+0

我在其中放置了一个打印文件,但它没有被调用。 – Martheli