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

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

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/

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:

确定