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