title: Swift LayoutConstraint breaks on keyboard frame change
body: <p>I have a simple ViewController with a <code>UITextView</code> inside of a <code>UIStackView</code>. I've also stored an <code>NSLayoutConstraint</code> which is the bottom anchor of the stackView.</p>
  <p>When the textView becomes first responder or resigns from first responder, two <code>Notification</code>s get triggered. The <code>keyboardDidShowNotification</code> works great. The issue is with <code>keyboardWillHideNotification</code>.</p>
  <p>I’ve noticed that when I change the keyboard's frame by clicking the emojis button and then dismiss the keyboard, something breaks with the constraints.</p>
  <p>As you can see in the gif below, as long as I don't go to the emojis section, the keyboard dismisses properly and the constraints are updated also properly. When I do go to the emojis section, then the <code>bottomStackViewBottomConstraint</code> and <code>schoolTextViewTopConstraint </code> go below that they should.</p>
  <p>Here's what happens;</p>
  <p><a href="https://i.stack.imgur.com/mmZqo.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mmZqo.gif" alt="enter image description here" /></a></p>
  <p>And here is the code;</p>
  <pre><code>fileprivate var schoolTextViewTopConstraint: NSLayoutConstraint!
  fileprivate var bottomStackViewBottomConstraint: NSLayoutConstraint!
  
  fileprivate func setupNotifications() {
      NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
      NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
  }
  
  @objc fileprivate func keyboardDidShow(notification: Notification) {
      if let infoKey = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey], let rawFrame = (infoKey as AnyObject).cgRectValue {
          let keyboardFrame = self.view.convert(rawFrame, from: nil)
              
          bottomStackViewBottomConstraint.constant = -(keyboardFrame.height) + self.view.safeAreaInsets.bottom
          schoolTextViewTopConstraint.constant = -keyboardFrame.height + 180
      }
          
      UIView.animate(withDuration: 0, animations: {
          self.sendCommentButton.isHidden = false
          self.toggleMuteButton.isHidden = true
          self.shareButton.isHidden = true
          self.leaveRoomButton.isHidden = true
              
          self.view.layoutIfNeeded()
      }, completion: nil)
  }
      
  @objc fileprivate func keyboardWillHide(notification: Notification) {
      schoolTextViewTopConstraint.constant = 0
      bottomStackViewBottomConstraint.constant = 0
          
      sendCommentButton.isHidden = true
          
      UIView.animate(withDuration: 0.5, animations: {
          self.toggleMuteButton.isHidden = self.currentUserRole == VoiceRoomRole.listener
          self.shareButton.isHidden = false
          self.leaveRoomButton.isHidden = false
              
          self.view.layoutIfNeeded()
      }, completion: nil)
  }
  </code></pre>