title: Animate viewController transition with AutoLayout constraints (Swift)
body: <p>I'm trying to animate viewControllers with the <code>UIViewControllerAnimatedTransitioning</code> delegate methods and I have some questions.</p>
  
  <p>All example codes that I'd seen use frames to animate views, but I'm using autolayout and it give me problems when animating frame changes.</p>
  
  <p>Having this two views:
  <a href="https://i.stack.imgur.com/SfL4b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SfL4b.png" alt="First View Controller"></a>
  <a href="https://i.stack.imgur.com/MwiIF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MwiIF.png" alt="Second View Controller"></a></p>
  
  <p>I want to animate transition from firstVC to secondVC in this way:</p>
  
  <p><a href="https://i.stack.imgur.com/219bh.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/219bh.gif" alt="Animation"></a></p>
  
  <p>So, this is the code I used to do this animation:</p>
  
  <pre><code>    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
  
      let containerView = transitionContext.containerView
      let fromVC = transitionContext.viewController(forKey: .from)! as! CustomView
      let toView = transitionContext.view(forKey: .to)!
      //let cell = fromVC.currentCell //Access to the pressed cell (if needed)
  
      toView.frame = fromVC.currentFrame //currentFrame is the cell frame saved on fromVC
  
      toView.autoresizingMask = containerView.autoresizingMask
      toView.layoutIfNeeded()
  
      containerView.addSubview(toView)
  
      UIView.animate(withDuration: duration, animations: {
  
          toView.frame = containerView.frame
          toView.layoutIfNeeded()
      }, completion: { _ in
  
          transitionContext.completeTransition(true)
      })
  }
  </code></pre>
  
  <p>As you can see, the animation is not working well because of frame modification and use of AutoLayout.</p>
  
  <p>So, how do you think I can do this animation with constraints?</p>
  
  <p>Thanks!</p>