【发布时间】:2018-08-02 21:33:52
【问题描述】:
我以编程方式创建的弹出窗口会填满屏幕。从示例代码中可以看出,我尝试了很多方法来限制它的大小,但都没有奏效。此外,没有调用委托方法。有任何想法吗?我过去曾成功地将 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")
})
}
}
【问题讨论】:
-
请记住,根据 Apple 文档,“在水平紧凑的环境中,.popover 选项的行为与 UIModalPresentationStyle.fullScreen 相同。” developer.apple.com/documentation/uikit/…
-
谢谢。看起来我为该任务选择了错误的组件。如果您发表评论和回答,我可以接受。
标签: swift uikit uipopoverpresentationcontroller