Permalink
Browse files

Handling keyboards better when rotating a view.

  • Loading branch information...
1 parent f511a56 commit 48196cc7c43119e57a535afbe4e9bc7ec65265e8 @devinross committed Jan 1, 2014
Showing with 27 additions and 13 deletions.
  1. +27 −13 src/TapkuLibrary/TKKeyboardTableViewController.m
@@ -34,6 +34,7 @@
@interface TKKeyboardTableViewController ()
@property (nonatomic,assign) BOOL scrollLock;
+@property (nonatomic,assign) CGRect keyboardRect;
@end
@implementation TKKeyboardTableViewController
@@ -76,22 +77,14 @@ - (void) keyboardWillAppear:(NSNotification*)sender{
self.scrollLock = YES;
- CGRect keyboardFrame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
- UIWindow *window = [UIApplication sharedApplication].windows[0];
- UIView *mainSubviewOfWindow = window.rootViewController.view;
- CGRect keyboardFrameConverted = [mainSubviewOfWindow convertRect:keyboardFrame fromView:window];
- CGRect rect = [self.view convertRect:keyboardFrameConverted fromView:mainSubviewOfWindow];
- rect = CGRectIntersection(rect, self.view.bounds);
-
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:0.05];
- [UIView setAnimationBeginsFromCurrentState:YES];
- [UIView setAnimationCurve:UIViewAnimationCurveLinear];
- self.tableView.contentInset = self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, 0, rect.size.height, 0);
- [UIView commitAnimations];
+ self.keyboardRect = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
+
+ [self _updateInsetWithKeyboard];
}
- (void) keyboardWillDisappear:(NSNotification*)sender{
+ self.keyboardRect = CGRectZero;
+
if(!self.isViewLoaded || self.view.superview == nil) return;
[UIView beginAnimations:nil context:nil];
@@ -112,6 +105,27 @@ - (void) textFieldDidBeginEditing:(UITextField *)textField{
self.scrollLock = YES;
[self performSelector:@selector(scrollToView:) withObject:textField afterDelay:0.1];
}
+- (void) _updateInsetWithKeyboard{
+ UIWindow *window = [UIApplication sharedApplication].windows[0];
+ UIView *mainSubviewOfWindow = window.rootViewController.view;
+ CGRect keyboardFrameConverted = [mainSubviewOfWindow convertRect:self.keyboardRect fromView:window];
+ CGRect rect = [self.view convertRect:keyboardFrameConverted fromView:mainSubviewOfWindow];
+ rect = CGRectIntersection(rect, self.view.bounds);
+
+
+
+ [UIView beginAnimations:nil context:nil];
+ [UIView setAnimationDuration:0.05];
+ [UIView setAnimationBeginsFromCurrentState:YES];
+ [UIView setAnimationCurve:UIViewAnimationCurveLinear];
+ self.tableView.contentInset = self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, 0, CGRectGetHeight(rect), 0);
+ [UIView commitAnimations];
+}
+
+#pragma mark Rotations
+- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
+ [self _updateInsetWithKeyboard];
+}

0 comments on commit 48196cc

Please sign in to comment.