如何更改Popover视图的大小

时间:2019-08-24 20:57:19

标签: ios swift iphone

当我按下当前视图上的按钮时,我试图布局一个弹出视图。使用以下代码可以很好地解决此问题,但问题是弹出窗口视图的大小不来自.preferredContentSize。

IngredientVC.preferredContentSize = CGSize(宽度:200,高度:300)

Popover视图具有当前视图的高度和宽度。如何缩小Popover视图的尺寸?

@IBAction func actionIngredients(_ sender: Any)
{
        // Load and configure your view controller.
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let IngredientVC = storyboard.instantiateViewController(
            withIdentifier: "testViewIdentifier")

        //let IngredientVC:testViewController = testViewController()

        // Use the popover presentation style for your view controller.
        IngredientVC.modalPresentationStyle = .popover
        IngredientVC.preferredContentSize = CGSize(width: 200, height: 300)

        // Specify the anchor point for the popover.
        //IngredientVC.popoverPresentationController?.barButtonItem =
        //optionsControl

        //let popovercontroller = optionsVC.popoverPresentationController
        IngredientVC.popoverPresentationController?.delegate = self
        IngredientVC.popoverPresentationController?.sourceView = self.view
        IngredientVC.popoverPresentationController?.sourceRect = CGRect(x:self.view.frame.width/2, y: self.view.frame.height/2,width: 200,height: 300)
        IngredientVC.popoverPresentationController?.permittedArrowDirections = [.down, .up]

        // Present the view controller (in a popover).
        self.present(IngredientVC, animated: true)
}

注意:我在情节提要中有一个弹出窗口视图控制器,其“模拟度量”大小为“自由格式”。

1 个答案:

答案 0 :(得分:1)

我发现我只需要遵循以下功能-

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