How to change the size of a popover

自作多情 提交于 2019-12-03 09:33:50

Set the preferred content size on the view controller being presented not the popoverPresentationController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) // func for popover
    {
        if segue.identifier == "popoverView"
        {
            let vc = segue.destinationViewController

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

            let controller = vc.popoverPresentationController

            controller?.delegate = self
            //you could set the following in your storyboard
            controller?.sourceView = self.view
            controller?.sourceRect = CGRect(x:CGRectGetMidX(self.view.bounds), y: CGRectGetMidY(self.view.bounds),width: 315,height: 230)
            controller?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)

        }
    }
Xeieshan

I fixed it via storyboard : Click on your controller Click on Attribute inspector ViewController> Check Use Preferred Explicit size and input values.

I'm not using storyboards. I just present a UINavigationController in the popover:

    self.present(popoverNavigationController!, animated: true) {}

The way to resize the popover size when a new view controller is pushed, it is just change the preferredContentSize before pushing it. For example:

    let newViewController = NewViewController()
    popoverNavigationController!.preferredContentSize = CGSize(width: 348, height: 400)
    popoverNavigationController!.pushViewController(newViewController, animated: true)

The problem is when we try to resize the popover when we pop a view controller.

If you use viewWillDisappear of the current view controller to change the preferredContentSize of the popover, the popover will resize but after the view controller is popped. That means that the animation has a delay.

You have to change the preferredContentSize before executing popViewController. That's mean you have to create a custom back button in the navigation bar like it is explained here. This is the implementation updated for Swift 4:

        self.navigationItem.hidesBackButton = true
        let newBackButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(CurrentViewController.backButtonTapped(sender:)))       
        self.navigationItem.leftBarButtonItem = newBackButton

And run the next code when the new Back button is pressed:

   @objc func backButtonTapped(sender: UIBarButtonItem) {

        self.navigationController?.preferredContentSize = CGSize(width: 348, height: 200)

        self.navigationController?.popViewController(animated: true)
   }

Basically, the preferredContentSize has to be changed before pushing and popping the view controller.

Kunsh Technologies

Above answers are correct which states about using the preferredContentSize, but the most important thing is to implement the protocol UIPopoverPresentationControllerDelegate and implement the below method otherwise it will not change the content size as expected.

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.none
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!