Ennabah
Ennabah

Reputation: 2533

Animating NSLayoutConstraint in didSelectRowAtIndexPath doesn't work

I have a MainVC(UIViewController) with a MainTableVC(UITableView) and another nested SubTableVC(UITableView).
Both of them has the MainVC as its delegate and dataSource, also, each one has its own restorationIdentifier so I can distinguish between them when I call a delegate or dataSource function.

Here's an example:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch tableView.restorationIdentifier {
    case "MainTableVCID":
        return 10
    case "SubTableVC":
        return 5
    default:
        return 0
    }
}

That's working perfectly great. But one issue I have here and I couldn't solve is:

I have a UIPickerView that's a subview of the MainVC's view, it's constrained just below the MainVC's view with constraints like this:

private let pickerView: UIPickerView = {
    let picker = UIPickerView()
    picker.backgroundColor = UIColor.lightGray
    picker.translatesAutoresizingMaskIntoConstraints = false
    return picker
}()

private var pickerViewBottomAnchor: NSLayoutConstraint?

override func viewDidLoad() {
    super.viewDidLoad()

    //other init code..

    view.addSubview(pickerView)
    pickerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    pickerView.heightAnchor.constraint(equalToConstant: 180).isActive = true
    pickerViewBottomAnchor = pickerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 180)
    pickerViewBottomAnchor?.isActive = true
    pickerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}

enter image description here

In the nested tableView (SubTableVC), in tableView:didSelectRowAtIndexPath: I want to animate the NSLayoutConstraint's constant to be equal to Zero.
However, at this time, I get pickerViewBottomAnchor = nil when I use console to print object (po self. pickerViewBottomAnchor?.constant) //prints nil.

Here's the code I use:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if tableView.restorationIdentifier == "SubTableVC" {
        pickerViewBottomAnchor?.constant = 180
        self.view.layoutIfNeeded()
    }
}

Upvotes: 0

Views: 53

Answers (1)

DonMag
DonMag

Reputation: 77690

Start with changing your didSelectRowAt function to:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if pickerViewBottomAnchor?.constant != 0 {
        pickerViewBottomAnchor?.constant = 0
    } else {
        pickerViewBottomAnchor?.constant = 180
    }

    UIView.animate(withDuration: 0.5) {
        self.view.layoutIfNeeded()
    }

}

Using that, selecting a row in either table should alternately slide the picker view up or down.

If that doesn't do anything, then you (likely) do not have your table views .delegate property set correctly.

If / when that does work, edit the function to only act when you've selected a row on the subTableView

Upvotes: 1

Related Questions