2017-08-10 116 views
0

我想用preferredContentSize(W:250,H:50)在中心顯示PopoverViewController,但它顯示該ViewController的完整大小,請找到我在哪裏做錯誤並糾正它。PopoverView Controller中的問題preferredContentSize

class ViewController: UIViewController, 
    UIPopoverControllerDelegate, UIPopoverPresentationControllerDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 

} 

@IBAction func PopBtn_Action(_ sender: Any) { 

    let popController = self.storyboard!.instantiateViewController(withIdentifier: "PincodeViewController") as! PincodeViewController 

    // set the presentation style 
    popController.modalPresentationStyle = UIModalPresentationStyle.popover 

    // set up the popover presentation controller 
    popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up 
    popController.popoverPresentationController?.delegate = self 
    popController.popoverPresentationController?.sourceView = sender as? UIView // button 
    popController.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.width/2, y: self.view.bounds.height/2, width: 1, height: 1) 

    popController.preferredContentSize = CGSize(width: 250, height: 250) 

    // present the popover 
    self.present(popController, animated: true, completion: nil) 

} 


func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle { 
    return .none 
} 

func popoverPresentationControllerShouldDismissPopover(popoverPresentationController: UIPopoverPresentationController) -> Bool { 
    return true 
} 

func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) { 

} 
+0

你是否檢查過'popController.popoverPresentationController?'不是'nil'? – clemens

回答

3

嘗試在迅速3.

func toShowPopOver() 
{ 
    let popoverContent = self.storyboard!.instantiateViewController(withIdentifier: "PincodeViewController") as! PincodeViewController 
    popoverContent.modalPresentationStyle = .popover 
    if let popover = popoverContent.popoverPresentationController { 
     popover.sourceView = self.view 
     popover.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0) 
     popover.sourceRect = CGRect(x: self.view.bounds.width/2, y: self.view.bounds.height/2, width: 1, height: 1) 
     popoverContent.preferredContentSize = CGSize(width: 50, height: 100) 
     popover.delegate = self 
    } 

    self.present(popoverContent, animated: true, completion: nil) 
} 


func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { 

    return UIModalPresentationStyle.none 
} 
下面的代碼
0

變化與此

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle 
{ 
    return UIModalPresentationStyle.none 
} 

它的工作對我的委託方法。希望這將有助於你..

+0

即使我的代碼也得到了很好的結果 –

相關問題