在iOS中,弹出窗口未显示在按钮附近或相对于按钮。

huangapple go评论117阅读模式
英文:

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,但仍然没有任何效果。还有其他可能导致这种情况的原因吗?

  1. lazy var launchButton: UIButton = {
  2. let button = UIButton(frame: CGRect(x: 150, y: 200, width: 100, height: 50))
  3. button.setTitle("Launch", for: .normal)
  4. button.addTarget(self, action:#selector(launchPopOver), for: .touchUpInside)
  5. return button
  6. }()
  7. @objc func launchPopOver() {
  8. let pop = PopViewController()
  9. pop.modalPresentationStyle = .popover
  10. pop.preferredContentSize.height = 250
  11. if let popPresentation = pop.popoverPresentationController {
  12. popPresentation.sourceView = launchButton
  13. popPresentation.sourceRect = launchButton.bounds
  14. }
  15. present(pop, animated: true, completion: nil)
  16. }
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. self.view.addSubview(launchButton)
  20. }
  21. class PopViewController: UIViewController, UIPopoverPresentationControllerDelegate {
  22. //
  23. public func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
  24. return .none
  25. }
  26. }
英文:

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?

  1. lazy var launchButton: UIButton = {
  2. let button = UIButton(frame: CGRect(x: 150, y: 200, width: 100, height: 50))
  3. button.setTitle("Launch", for: .normal)
  4. button.addTarget(self, action:#selector(launchPopOver), for: .touchUpInside)
  5. return button
  6. }()
  7. @objc func launchPopOver() {
  8. let pop = PopViewController()
  9. pop.modalPresentationStyle = .popover
  10. pop.preferredContentSize.height = 250
  11. if let popPresentation = pop.popoverPresentationController {
  12. popPresentation.sourceView = launchButton
  13. popPresentation.sourceRect = launchButton.bounds
  14. }
  15. present(pop, animated: true, completion: nil)
  16. }
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. self.view.addSubView(launchButton)
  20. }
  21. class PopViewController: UIViewController, UIPopoverPresentationControllerDelegate {
  22. //
  23. public func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
  24. return .none
  25. }
  26. }

答案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/

huangapple
  • 本文由 发表于 2023年6月12日 17:18:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定