修改 Popover View Controller 的宽度

Modify width of Popover View Controller

我正在尝试修改我的弹出窗口的宽度,它是 UITableViewController,这样它只占父视图宽度的一半。当点击另一个 UITableView(父视图)中的按钮时,将以编程方式调用弹出窗口。我尝试设置弹出窗口的 preferredContentSize 并设置 sourceRect 但弹出窗口仍然占据整个屏幕。

class MyTableViewController: UITableViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIDynamicAnimatorDelegate, UIGestureRecognizerDelegate, CLLocationManagerDelegate, UIPopoverPresentationControllerDelegate, UIAdaptivePresentationControllerDelegate {

...

func goToPlaces(button: UIButton) {

        let fromRect = CGRectMake(50.0, 50.0, self.view.bounds.width / 2.0, self.view.bounds.height)
        let popoverVC = storyboard?.instantiateViewControllerWithIdentifier("otherPlaces")
        popoverVC?.modalPresentationStyle = .OverFullScreen
        presentViewController(popoverVC!, animated: true, completion: nil)
        popoverVC?.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
        popoverVC?.preferredContentSize = CGSizeMake(self.view.bounds.width / 2.0, self.view.bounds.height)
        let popoverController = popoverVC?.popoverPresentationController
        popoverPresentationController?.sourceView = self.view
        popoverPresentationController?.sourceRect = fromRect
        popoverController?.permittedArrowDirections = .Any
        popoverController?.delegate = self


    }

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

    }

编辑: 当我打印 popoverPresentationController?.sourceViewpopoverPresentationController?.sourceRect

由于某种原因,它们都return为零

尝试使用 popoverVC.modalPresentationStyle = UIModalPresentationStyle.PageSheet

您要的是

popoverVC?.modalPresentationStyle = .OverFullScreen

所以你让它覆盖整个屏幕。尝试使用:

popoverVC?.modalPresentationStyle = .Popover

presentViewController(popoverVC!, animated: true, completion: nil)

也应该是最后一个,这样代表就可以得到它想要响应的调用。 (我认为 - UIKit 是否真的延迟了演示可能无关紧要。)