当源视图在Swift中更改它们时,如何更新popover的布局(位置)?

时间:2018-05-06 19:13:44

标签: ios swift autolayout uikit uipopoverpresentationcontroller

我想更新popover的布局,我已经尝试使用setNeedsLayout()layoutIfNeeded()方法,但找不到解决方案。

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

  1. 按钮 - >隐藏button3,并将button1和button2移动到左侧。
  2. 重新定位 - >将按钮的源视图从button1 / 2/3/4(发送者)更改为button4。 Picture of the layout
  3. 我遇到的问题是我无法更新/刷新屏幕,并且popover始终指向前一个源视图位置。 Picture of the popover

    不知何故,我需要一个更新视图的方法,它比layoutIfNeeded()强,因为如果屏幕从纵向/横向更改,则弹出框会自动更改。 Here I changed portrait/landscape/portrait

    项目的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()
        }
    }
    

2 个答案:

答案 0 :(得分:0)

您需要指定箭头的新位置并询问演示控制器视图的布局,而不仅仅是呈现的视图。

尝试:

func repositionPopover() {
    let popOverController = presentedViewController?.presentationController as? UIPopoverPresentationController
    popOverController?.sourceView = button4
    popOverController?.containerView?.setNeedsLayout()
    popOverController?.containerView?.layoutIfNeeded()
}

(无需在主线程上发送,使用UIKit时已经在该线程上)

答案 1 :(得分:0)

您可以尝试再次设置sourceview和sourcerect。当sourcview边界更改时,调用此函数。