title: Swift Scrollview scroll so that button is right above keyboard when keyboard appears
body: <p>I have tried literally every solution on the internet to get this answer, and none of them work without bugs. I have a stackview embedded in a scrollview, and I want the <code>signInButton</code> to be right above the keyboard.</p>
  
  <p>Here is my current attempted solution:</p>
  
  <pre><code>@objc func keyboardWillShow(notification: NSNotification) {
      guard let userInfo: NSDictionary = notification.userInfo as NSDictionary?,
          let keyboardInfo = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
              return
      }
      let keyboardSize = keyboardInfo.cgRectValue.size
      scrollView.isScrollEnabled = false
      let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize.height, right: 0.0)
      self.scrollView.contentInset = contentInsets
      self.scrollView.scrollIndicatorInsets = contentInsets
      var rect = self.view.frame
      rect.size.height -= keyboardSize.height
      var frame = self.signInButton.frame
      frame.size.height += 50
      self.scrollView.scrollRectToVisible(frame, animated: true)
  }
  @objc func keyboardWillHide(notification: NSNotification) {
      let contentInset:UIEdgeInsets = UIEdgeInsets.zero
      scrollView.contentInset = contentInset
      scrollView.isScrollEnabled = false
  }
  </code></pre>
  
  <p>But this does not come close to working on all screen sizes. While the button always appears sometimes it appears way above the keyboard, and other times slightly above. And when the keyboard disappears on some screens it doesn't work either. I'm curious if anyone knowledgeable can give a robust answer that many can use for this ubiquitous problem. Swift 5 please. </p>
  
  <p>I know I can use a tableview, but I find it incredibly sad that iOS does not have a clear, alternative working solution without embedding everything in a tableview.</p>
  
  <p>Thanks!</p>