PopoverView 控制器中的问题 PreferredContentSize

Posted

技术标签:

【中文标题】PopoverView 控制器中的问题 PreferredContentSize【英文标题】:Issues in PopoverView Controller preferredContentSize 【发布时间】:2017-08-10 06:32:23 【问题描述】:

我想用preferredContentSize(W: 250, H: 50)在中心显示PopoverViewController,但是它显示的是那个ViewController的全尺寸,请找到我做错的地方并纠正它。

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) 


【问题讨论】:

你检查过popController.popoverPresentationController?不是nil吗? 【参考方案1】:

在 swift 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

【讨论】:

【参考方案2】:

用这个改变你的委托方法

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle

    return UIModalPresentationStyle.none

它对我有用..希望这对你有帮助..

【讨论】:

以上是关于PopoverView 控制器中的问题 PreferredContentSize的主要内容,如果未能解决你的问题,请参考以下文章

带有自己的 XIB 的 PopoverView 中的 UIButton 应将操作发送到主视图

将导航控制器用于 uipopoverview 的最佳方式

iPhone上的uibarbutton和popoverview

textview上的popoverview单击tableview单元格

如何在popoverview中显示两个按钮-WEPOPOVER

无法导航到根视图控制器