简体   繁体   中英

How to change size of Popover view

I am trying to lay out a popover view when a button on current view is pressed. This works fine with following code but problem is that the popover view does not take the size from .preferredContentSize.

IngredientVC.preferredContentSize = CGSize(width: 200, height: 300)

Popover view has height and width of current view. How do I make Popover view smaller size please?

@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)
}

Note: I have popover view controller in Storyboard with Simulated Metrics size as "Freeform".

I figured out that all I needed was following function -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM