修改Popover View Controller的宽度

时间:2015-09-26 05:29:06

标签: ios swift uitableview swift2 uipopovercontroller

我正在尝试修改我的popover的宽度,这是UITableViewController,因此它只占用父视图宽度的一半。当点击另一个UITableView(父视图)中的按钮时,以编程方式调用popover。我尝试设置弹出框的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

由于某种原因,他们都返回零

2 个答案:

答案 0 :(得分:0)

尝试使用popoverVC.modalPresentationStyle = UIModalPresentationStyle.PageSheet

答案 1 :(得分:0)

你要求

popoverVC?.modalPresentationStyle = .OverFullScreen

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

popoverVC?.modalPresentationStyle = .Popover

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

也应该是最后一个,以便委托可以获得它想要响应的调用。 (我想 - 如果UIKit实际上推迟了演示文稿,那可能并不重要。)