以编程方式创建的UIPopoverPresentationController填充屏幕

时间:2018-08-02 21:33:53

标签: swift uikit uipopoverpresentationcontroller

我正在以编程方式创建的弹出窗口充满了屏幕。从示例代码中可以看到,我尝试了多种方法来限制其大小,但是没有一种起作用。同样,不调用委托方法。有任何想法吗?过去,我已经成功使用CALayer进行这种操作,但是认为这样做会更简单-也许不是。

@objc func touchDownHandler(sender: UISlider) {
    let popoverController = UIViewController()
    popoverController.view.backgroundColor = .red
    popoverController.view.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
    let textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    textLabel.text = "Hello World"
    textLabel.backgroundColor = .green
    popoverController.view.addSubview(textLabel)

    popoverController.modalPresentationStyle = UIModalPresentationStyle.popover
    popoverController.preferredContentSize = CGSize(width: 200, height: 200)

    if let popoverPresentation = popoverController.popoverPresentationController {
        popoverPresentation.delegate = self
        popoverPresentation.sourceRect = sender.frame
        popoverPresentation.popoverLayoutMargins = UIEdgeInsets(top: 10, left: 10, bottom: 210, right: 210)
        popoverPresentation.backgroundColor = .blue
        self.controller.present(popoverController, animated: true, completion: {
            print("pop over is visible")
        })
    }
}

1 个答案:

答案 0 :(得分:0)

请记住,按照Apple文档的说明,“在水平紧凑的环境中,.popover选项的行为与UIModalPresentationStyle.fullScreen相同。” https://developer.apple.com/documentation/uikit/uimodalpresentationstyle/popover