Swift: Change autolayout constraints when keyboard is shown
<p>I am trying to have the bottom of a <code>UItextView</code> change constraints when the keyboard is presented so the <code>UItextView</code>is "constrained" by the textView.  I have a <code>UIView</code> I have named spacer in between the <code>UItextView</code> and the bottom layout guide.  I am trying to change the value of the constraint for the spacer to the bottom of the view to the keyboard height.  Any help is appreciated.</p>

<p>Here is my code:</p>

<pre><code>@IBOutlet weak var textView: UITextView!    
@IBOutlet weak var spacer: UIView!
@IBOutlet weak var spacerBottomLayoutConstraint: NSLayoutConstraint!
</code></pre>

<p>in <code>viewDidLoad</code>:</p>

<pre><code>textView.becomeFirstResponder()

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AdditionalDetailsVC.keyboardShown(_:)), name: UIKeyboardDidShowNotification, object: nil)
</code></pre>

<p>and where the magic happens:</p>

<pre><code>func keyboardShown(notification: NSNotification) {

    let info  = notification.userInfo!

    let value: AnyObject = info[UIKeyboardFrameEndUserInfoKey]!

    let rawFrame = value.CGRectValue

    keyboardFrame = view.convertRect(rawFrame, fromView: nil)

    print(keyboardFrame)

    print(spacerBottomLayoutConstraint)

    //Setting text field to bottom of keyboard
    //spacerBottomLayoutConstraint.constant = keyboardFrame.height

    UIView.animateWithDuration(1, animations: {

        self.spacerBottomLayoutConstraint.constant = self.keyboardFrame.height

        self.view.layoutIfNeeded()
    })
}
</code></pre>