0

我以编程方式创建的弹出窗口填满了屏幕。从示例代码中可以看出,我尝试了很多方法来限制它的大小,但都没有奏效。此外,没有调用委托方法。有任何想法吗?我过去曾成功地将 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")
        })
    }
}
4

1 回答 1

0

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

于 2018-08-05T23:48:20.987 回答