2

我想更新弹出框的布局,我已经尝试过使用setNeedsLayout()andlayoutIfNeeded()方法但找不到解决方案。

我有一个简单的屏幕,有 4 个调用弹出框的按钮,在弹出框内有 2 个额外的按钮:

  1. Button -> 隐藏 button3,并将 button1 和 button2 向左移动。
  2. 重新定位 -> 将弹出框的源视图从 button1/2/3/4(发送者)更改为 button4。 布局图

我遇到的问题是我无法更新/刷新屏幕,并且弹出框一直指向以前的源视图位置。 弹出框的图片

不知何故,我需要一种方法来更新我的视图,并且比layoutIfNeeded()因为如果屏幕从纵向/横向更改,弹出框会自动更改。在这里我改变了肖像/风景/肖像

项目的 GitHub。GitHub

使用的代码如下:

class ViewController: UIViewController, ButtonDidDisappearDelegate {

    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var button2: UIButton!
    @IBOutlet weak var button3: UIButton!
    @IBOutlet weak var button4: UIButton!

    @IBOutlet weak var constrain2: NSLayoutConstraint!

    var popVC: PopVC?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func buttonPopover(_ sender: UIButton) {
        configureAndPresentPopover(from: sender)
    }

    func buttonDisappear() {
        self.constrain2.constant = 100
        self.button3.isHidden = !self.button3.isHidden

        if !self.button3.isHidden {
            self.constrain2.constant = 10
        }
    }

    func repositionPopover() {
        DispatchQueue.main.async {
            if let popVC = self.popVC {
                self.self.popoverPresentationController(popVC.popoverPresentationController!, willRepositionPopoverTo: self.button4.bounds, in: self.button4)
            }
            self.view.setNeedsLayout()
            self.view.layoutIfNeeded()
        }
    }
}

extension ViewController: UIPopoverPresentationControllerDelegate {

    func configureAndPresentPopover(from sender: UIButton) {

        popVC = storyboard?.instantiateViewController(withIdentifier: "popVC") as? PopVC

        guard let popVC = popVC else {
            return
        }

        popVC.popOverDelegate = self
        popVC.modalPresentationStyle = .popover

        let popOverVC = popVC.popoverPresentationController
        popOverVC?.delegate = self
        popOverVC?.sourceView = sender
        popOverVC?.sourceRect = sender.bounds
        popVC.preferredContentSize = CGSize(width: 300, height: 60)
        popOverVC?.permittedArrowDirections = .down

        self.present(popVC, animated: true)
    }

    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }

    func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController,
                                       willRepositionPopoverTo rect: CGRect,
                                       in view: UIView) {
        popoverPresentationController.sourceView = view
        popoverPresentationController.sourceRect = rect
    }
}

和弹出控制器:

protocol ButtonDidDisappearDelegate: class {
    func configureAndPresentPopover(from sender: UIButton)
    func buttonDisappear()
    func repositionPopover()
}

class PopVC: UIViewController {

    weak var popOverDelegate: ButtonDidDisappearDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
    }


    @IBOutlet weak var popoverButton: UIButton!

    @IBAction func popoverAction(_ sender: Any) {
        popOverDelegate?.buttonDisappear()
        DispatchQueue.main.async {
            self.view.setNeedsLayout()
            self.view.layoutIfNeeded()
        }
    }

    @IBAction func reposition(_ sender: Any) {
        popOverDelegate?.repositionPopover()
    }
}
4

2 回答 2

3

您需要指定箭头的新位置并要求展示控制器视图的布局,而不仅仅是展示的视图。

尝试 :

func repositionPopover() {
    let popOverController = presentedViewController?.presentationController as? UIPopoverPresentationController
    popOverController?.sourceView = button4
    popOverController?.containerView?.setNeedsLayout()
    popOverController?.containerView?.layoutIfNeeded()
}
于 2018-05-09T08:04:35.263 回答
0

您可以尝试再次设置 sourceview 和 sourcerect。当源视图边界改变时调用这个函数。

于 2020-11-18T13:04:39.547 回答