2017-07-19 94 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