aaronmhr
aaronmhr

Reputation: 21

How to update the layout (position) of a popover when its source view changes theirs in Swift?

I want to update the layout of a popover, I've already tried using the setNeedsLayout() and layoutIfNeeded() methods but couldn't find a solution.

I have a simple screen with 4 buttons that call a popover, within the popover there are 2 extra buttons:

  1. Button -> hides button3, and moves button1 and button2 to the left.
  2. reposition -> changes the sourceview of the popover from the button1/2/3/4 (sender) to button4. Picture of the layout

The problem I have is that I'm not able to update/refresh the screen, and the popover keeps pointing to the former source view position. Picture of the popover

Somehow I need a method that updates my view and which is stronger than layoutIfNeeded() because if the screen is changed from portrait/landscape the popover changes automatically. Here I changed portrait/landscape/portrait

GitHub of the project. GitHub

The code used is the following:

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
    }
}

and the popover controller:

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()
    }
}

Upvotes: 2

Views: 1794

Answers (2)

GaétanZ
GaétanZ

Reputation: 4930

You need to specify the new position of the arrow and ask for the layout of the presentation controller's view, not only the presented view.

Try :

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

Upvotes: 4

Preetham
Preetham

Reputation: 1

You can try setting the sourceview and sourcerect once again. call this function when the sourcview bounds are changed.

Upvotes: 0

Related Questions