title: Move a view up when the keyboard covers an input field but with leaving some space between them
body: <h1>Description</h1>
  <p>I have a few UITextField which I want to scroll up, when one of them is covered by keyboard during edition. There are a tons of answers here on SO with many flavors: moving a view (by changing its frame), modifying a constraint, using UIScrollView and UITableView, or using UIScrollView and modifying contentInset.</p>
  <p>I decided to use the last one. This one is also <a href="https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html" rel="nofollow noreferrer">described by Apple</a>,  and has a <a href="https://stackoverflow.com/a/28813720/1656515">Swift version on SO</a> as well as being <a href="https://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios" rel="nofollow noreferrer">described on this blog</a> including a <a href="https://github.com/woelmer/UIScrollViewWithAutoLayout" rel="nofollow noreferrer">sample project on the GitHub</a>.</p>
  <h3>Partial code</h3>
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      NSNotificationCenter.defaultCenter().addObserver(self, selector: &quot;keyboardWillShow:&quot;, name: UIKeyboardWillShowNotification, object: nil)
      NSNotificationCenter.defaultCenter().addObserver(self, selector: &quot;keyboardWillBeHidden:&quot;, name: UIKeyboardWillHideNotification, object: nil)
  }
  
  func textFieldShouldReturn(textField: UITextField) -&gt; Bool {
      textField.resignFirstResponder()
      return true
  }
  
  func textFieldDidEndEditing(textField: UITextField) {
      self.activeField = nil
  }
  
  func textFieldDidBeginEditing(textField: UITextField) {
      self.activeField = textField
  }
  
  func keyboardWillShow(notification: NSNotification) {
      if let activeField = self.activeField, keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
          let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize.height, right: 0.0)
          self.scrollView.contentInset = contentInsets
          self.scrollView.scrollIndicatorInsets = contentInsets
          var aRect = self.view.frame
          aRect.size.height -= keyboardSize.size.height
          if (!CGRectContainsPoint(aRect, activeField.frame.origin)) {
              self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
          }
      }
  }
  
  func keyboardWillBeHidden(notification: NSNotification) {
      let contentInsets = UIEdgeInsetsZero
      self.scrollView.contentInset = contentInsets
      self.scrollView.scrollIndicatorInsets = contentInsets
  }
  </code></pre>
  <h1>Issue</h1>
  <p>I want just one simple modification - a little more space between the keyboard and an edited field. Because out of the box it looks like this:</p>
  <p><a href="https://i.stack.imgur.com/vovnl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vovnl.png" alt="screenshot" /></a></p>
  <p>I modified <code>CGRect</code> in <code>scrollRectToVisible</code> call, but it changed nothing. What's more, even commenting out the line with <code>scrollRectToVisible</code> had no effect at all - everything worked exactly as before (including scrolling up the content). Checked on iOS 9.2</p>
  <p>Replicating, if needed, is trivial - just download the working code using the GitHub link above and comment out the scrollRectToVisible line.</p>
  <h3>Tested workarounds</h3>
  <p>Workarounds I tried, but didn't like the final effect:</p>
  <ul>
  <li>Increasing <code>contentInset</code> - user could scroll up more then the contentSize</li>
  <li>Replacing <code>UIKeyboardDidShowNotification</code> with <code>UIKeyboardWillShowNotification</code>, add another <code>UIKeyboardDidShowNotification</code> observer with just <code>scrollRectToVisible</code> inside - it works, but then there are two scroll up animations, which doesn't look good.</li>
  </ul>
  <h1>Questions</h1>
  <ul>
  <li>Why changing <code>contentInset</code> (without <code>scrollRectToVisible</code> call) scrolls content in the scrollView? I've not see in any docs information about such behavior</li>
  <li>And more important - what to do, to scroll it up a little more, to have some space between an edited text field and the keyboard?</li>
  </ul>
  <p>What did I miss? Is there some easy way to fix it? Or maybe it's better to play with <code>scrollView.contentSize</code>, instead of <code>contentInset</code>?</p>