英文:
Popover in iOS not displayed near or relative to a button
问题
I am trying to display a popOver next/relative to a UIButton. My current popOver always defaults to the top left corner. Even tried setting sourceRect, sourceView but still it makes no difference. Any idea what else can cause this?
尝试在UIButton旁边/相对于其显示弹出窗口。我的当前弹出窗口始终默认在左上角。甚至尝试设置sourceRect、sourceView,但仍然没有任何效果。还有其他可能导致这种情况的原因吗?
lazy var launchButton: UIButton = {
let button = UIButton(frame: CGRect(x: 150, y: 200, width: 100, height: 50))
button.setTitle("Launch", for: .normal)
button.addTarget(self, action:#selector(launchPopOver), for: .touchUpInside)
return button
}()
@objc func launchPopOver() {
let pop = PopViewController()
pop.modalPresentationStyle = .popover
pop.preferredContentSize.height = 250
if let popPresentation = pop.popoverPresentationController {
popPresentation.sourceView = launchButton
popPresentation.sourceRect = launchButton.bounds
}
present(pop, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(launchButton)
}
class PopViewController: UIViewController, UIPopoverPresentationControllerDelegate {
//
public func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
}
英文:
I am trying to display a popOver next/relative to a UIButton. My current popOver always defaults to the top left corner. Even tried setting sourceRect, sourceView but still it makes no difference. Any idea what else can cause this?
lazy var launchButton: UIButton = {
let button = UIButton(frame: CGRect(x: 150, y: 200, width: 100, height: 50))
button.setTitle("Launch", for: .normal)
button.addTarget(self, action:#selector(launchPopOver), for: .touchUpInside)
return button
}()
@objc func launchPopOver() {
let pop = PopViewController()
pop.modalPresentationStyle = .popover
pop.preferredContentSize.height = 250
if let popPresentation = pop.popoverPresentationController {
popPresentation.sourceView = launchButton
popPresentation.sourceRect = launchButton.bounds
}
present(pop, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubView(launchButton)
}
class PopViewController: UIViewController, UIPopoverPresentationControllerDelegate {
//
public func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
}
答案1
得分: 1
我的第一个猜测是,你不应该使用 launchButton.bounds
,而应该使用 launchButton.frame
作为 sourceRect
值,因为 bounds 通常会解析为具有位置 { x: 0, y: 0 }
,这可以解释为什么会出现“始终默认为左上角”的情况。
有关更多详情,你可以阅读这篇文章:https://programmingwithswift.com/difference-between-frame-and-bounds-in-swift/
英文:
My first guess is that you shouldn't be using launchButton.bounds
but launchButton.frame
instead for the sourceRect
value as bounds will typically resolve to having position of { x: 0, y: 0 }
which would explain the
> "always defaults to the top left corner"
For more details you can read this article https://programmingwithswift.com/difference-between-frame-and-bounds-in-swift/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论