Clifton Labrum
Clifton Labrum

Reputation: 14060

Animating Auto Layout Constraints Not Working

iOS 10, Swift 3.1

I have a UIView called graphPopup inside a UITableViewCell that I'm trying to animate. Basically when someone taps a graph, a popup appears and I'd like it to animate its x and y into place.

Everything is working except the animation of the popup. Here's how it looks now: enter image description here

I want the popup to slide from point-to-point into position. Here is my code:

class TotalCell: UITableViewCell, ChartViewDelegate{

  @IBOutlet weak var graphPopup: UIView!
  @IBOutlet weak var popupConstraintY: NSLayoutConstraint!
  @IBOutlet weak var popupConstraintX: NSLayoutConstraint!

  func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
    //...
    graphPopup.isHidden = false

    //Set new auto layout constraints
    self.popupConstraintX.constant = highlight.xPx-44
    self.popupConstraintY.constant = highlight.yPx+18

    //Animate (which does nothing)   
    UIView.animate(withDuration: 1.0, animations: {
      self.graphPopup.layoutIfNeeded()
    })
  }
}

Any idea what I might be doing wrong? If I remove the animation bit entirely, it behaves the same way, but I know the animation is being called.

Any ideas?

Upvotes: 2

Views: 1095

Answers (1)

Bart Whiteley
Bart Whiteley

Reputation: 1426

Try self.layoutIfNeeded() instead of self.graphPopup.layoutIfNeeded().

Upvotes: 6

Related Questions