Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollViewKeyboardDismissModeInteractive changing tableview height with keyboard

Within a UIViewController I've set self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive.

This is great because the user can drag the keyboard down from the tableview.

However, the tableview maintains it's current height when dragging down the keyboard. This looks odd because it leaves empty space between the keyboard and the scrollview.

How can I persistently track the frame of the keyboard so I may resize the tableview as the user drags the keyboard? I've tried using UIKeyboardWillChangeFrameNotification but that seems to only get called after the user finishes dragging.

like image 987
Awesome-o Avatar asked Dec 18 '14 21:12

Awesome-o


2 Answers

Your table view shouldn't change its height to accommodate the keyboard.

Instead, the keyboard should be presented overtop of the table view, and you should adjust the contentInset and scrollIndicatorInsets properties on the table view so that the lower table content is not obscured by the keyboard. You need to update the scroll insets whenever the keyboard is presented or dismissed.

You don't have to do anything special while the keyboard is dismissed interactively, because the table content should already scroll down as the keyboard moves out of view.

like image 123
Darren Avatar answered Sep 28 '22 02:09

Darren


I'd rather this not be the accepted answer, but for those of you out there also having trouble with this here's what worked for me.

  1. Create a custom subclass of UIView.

  2. In the subclass's willMoveToSuperview: method, add a KVO observer to the superview's center on iOS 8 and frame on lesser versions of iOS (remember to remove the old observer, you may want to use an instance variable to track this).

  3. In your view controller add a 0.0 height input accessory view to the view controller via inputAccessoryView class override. Use your custom UIView subclass for this.

  4. Back in the subclass, in observeValueForKeyPath:..., capture the frame of the view's superview, this should be the frame of the UIKeyboard.

  5. Use NSNotificationCenter or some other means to then pass this frame back to your view controller for processing.

It's a pretty painful process and not guaranteed to work in future versions of iOS. There are likely a ton of edge cases that will pop up later since I just built this, but it's a good start. If anyone has a better idea I'll happily mark your answer as correct.

like image 37
Awesome-o Avatar answered Sep 28 '22 04:09

Awesome-o