1

I am animating a button to move from the center of a view to a corner and want to know how to make it stay there after I rotate the device. I mocked up a very simple app to show the issue. Here is the example storyboard:

example storyboard

The constraints are added to the appropriate IBOutletCollection in the ViewController and the Button action is linked to the IBAction sendToCorner in the example ViewController:

import UIKit

class ViewController: UIViewController {
    @IBOutlet var centerConstraints: [NSLayoutConstraint]!
    @IBOutlet var cornerConstraints: [NSLayoutConstraint]!

    @IBAction func sendToCorner() {
        NSLayoutConstraint.deactivate(centerConstraints)
        NSLayoutConstraint.activate(cornerConstraints)
        UIView.animate(withDuration: 0.5) {
            self.view.layoutIfNeeded()
        }
    }
}

The animation from the center to the corner works. However if I rotate the device after I send the button to the corner it returns to the center. My question is: how to permanently send the button to the corner with an animation? Or more generically, how to animate constraints and make the constraints active state permanent through device rotations.

Habib Alejalil
  • 435
  • 1
  • 4
  • 17
Joshua
  • 165
  • 2
  • 11
  • I am using Xcode 9.0 and targeting iOS 11 – Joshua Oct 13 '17 at 17:18
  • 1
    Possible duplicate of [Keep autolayout constraints active status on device rotation](https://stackoverflow.com/questions/35130999/keep-autolayout-constraints-active-status-on-device-rotation) – Tamás Sengel Oct 13 '17 at 17:19

0 Answers0