小编典典

如何在iOS 8中正确显示弹出框

swift

我正在尝试将UIPopoverView添加到我的Swift iOS
8应用程序中,但由于弹出框未以正确的形状显示,因此无法访问PopoverContentSize属性。我的代码:

var popover: UIPopoverController? = nil

    func addCategory() {

    var newCategory = storyboard.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
    var nav = UINavigationController(rootViewController: newCategory)
    popover = UIPopoverController(contentViewController: nav)
    popover!.setPopoverContentSize(CGSizeMake(550, 600), animated: true)
    popover!.delegate = self
    popover!.presentPopoverFromBarButtonItem(self.navigationItem.rightBarButtonItem, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}

输出:

在此处输入图片说明

当我通过UIPopoverPresentationController做同样的事情时,我仍然没有完成它。这是我的代码:

func addCategory() {

    var popoverContent = self.storyboard.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
    var nav = UINavigationController(rootViewController: popoverContent)
    nav.modalPresentationStyle = UIModalPresentationStyle.Popover
    var popover = nav.popoverPresentationController as UIPopoverPresentationController
    popover.delegate = self
    popover.popoverContentSize = CGSizeMake(1000, 300)
    popover.sourceView = self.view
    popover.sourceRect = CGRectMake(100,100,0,0)

    self.presentViewController(nav, animated: true, completion: nil)

}

我得到完全相同的输出。

如何自定义弹出窗口的大小?任何帮助将不胜感激!


阅读 263

收藏
2020-07-07

共1个答案

小编典典

好的,一位室友看了看,发现:

 func addCategory() {

    var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
    var nav = UINavigationController(rootViewController: popoverContent)
    nav.modalPresentationStyle = UIModalPresentationStyle.Popover
    var popover = nav.popoverPresentationController
    popoverContent.preferredContentSize = CGSizeMake(500,600)
    popover.delegate = self
    popover.sourceView = self.view
    popover.sourceRect = CGRectMake(100,100,0,0)

    self.presentViewController(nav, animated: true, completion: nil)

}

就是这样做。

您不再需要与popover本身对话,而是与其中的View Controller对话,以通过调用属性来设置内容大小
preferredContentSize

2020-07-07