1

I’m trying to move the content up when the keyboard appears. This is the content which is a simple login form.

enter image description here enter image description here

Please note that these are not UITextFields. Its just a small UITableView in the middle of a UIViewController. And I have a UIScrollView filling the view controller embedding that table view.

I’ve registered for the keyboard notifications, UIKeyboardDidShowNotification and UIKeyboardWillHideNotification.

And in the method that fires when the keyboard appears, I've implemented a variation of this answer which does pretty much the same thing, setting the bottom value of the UIEdgeInsets which is used to set the contentInset of the scroll view.

func keyboardWasShown(notification: NSNotification) {

    var info: NSDictionary = notification.userInfo as NSDictionary
    var keyboardSize: CGSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey).CGRectValue().size

    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
}

The problem is it doesn't do anything. I successfully get the height of the keyboard but nothing changes. Can anybody tell me why and what I should do to fix this please?

Thank you.

Community
  • 1
  • 1
Isuru
  • 27,485
  • 56
  • 174
  • 278
  • check this answer, it for ObjC, but the logic is the same: http://stackoverflow.com/questions/23988866/how-do-i-resize-views-when-keyboard-pops-up-using-auto-layout/23992055#23992055 – holex Aug 05 '14 at 10:19

3 Answers3

2
UIEdgeInsets UIEdgeInsetsMake (
   CGFloat top,
   CGFloat left,
   CGFloat bottom,
   CGFloat right
);

You are setting the bottom. Try setting the top insets. Maybe a negative value will help.

The dude
  • 7,748
  • 1
  • 22
  • 50
  • Setting a negative value to the top onset worked but I didn't want to hardcode values. That's why I was looking for a more cleaner method. I managed to do it by using a different method. I posted my answer here. – Isuru Aug 05 '14 at 13:09
2

After much struggle, I was able to accomplish what I wanted. What I did was, in the viewDidLoad I registered for the keyboard notifications. And when the keyboard appears, I got its frame in to a variable and the login form's (which is a table view) frame into another variable.

By using CGRectIntersection I got the frame of the intersected portion when the keyboard overlaps the login form and go its height. Then I simply set it in scroll view's setContentOffset method to automatically move the scroll view up. To move it back down to the original position I just set scroll view's contentOffset property again to CGPointZero.

func registerForKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
}

func keyboardWasShown(notification: NSNotification) {
    var info: NSDictionary = notification.userInfo as NSDictionary
    var keyboardFrame: CGRect = info.objectForKey(UIKeyboardFrameEndUserInfoKey).CGRectValue()
    var loginFormFrame: CGRect = self.view.convertRect(self.tableView.frame, fromView: nil)
    var coveredFrame: CGRect = CGRectIntersection(loginFormFrame, keyboardFrame)

    self.scrollView.setContentOffset(CGPointMake(0, coveredFrame.height + 20), animated: true)
}

func keyboardWillBeHidden(notification: NSNotification) {
    self.scrollView.contentOffset = CGPointZero
}
Isuru
  • 27,485
  • 56
  • 174
  • 278
-1

ContentInset means The distance that the content view is inset from the enclosing scroll view. What you need to do is to change the origin.y value.

See How to make a UITextField move up when keyboard is present?

Community
  • 1
  • 1
Allen
  • 6,257
  • 4
  • 38
  • 54
  • the `contentInset` has been designed especially handling the appearing keyboards on the screen without changing the `UIScrollView` instance's `frame` – so there is nothing to do with the `frame` at all, you should have linked this one instead: http://stackoverflow.com/questions/23988866/how-do-i-resize-views-when-keyboard-pops-up-using-auto-layout/23992055#23992055 – holex Aug 05 '14 at 10:16
  • @Allen: I saw that thread before but the solutions there was of no use because I don't have any `UITextField`s in the view. – Isuru Aug 05 '14 at 13:12
  • @holex I wanted to actually move up the scroll view automatically instead of enabling the user to scroll up. I managed to achieve this. I posted my answer here. – Isuru Aug 05 '14 at 13:13