diff --git a/TPKeyboardAvoiding/TPKeyboardAvoidingCollectionView.m b/TPKeyboardAvoiding/TPKeyboardAvoidingCollectionView.m index caad404e..3c53b6c9 100644 --- a/TPKeyboardAvoiding/TPKeyboardAvoidingCollectionView.m +++ b/TPKeyboardAvoiding/TPKeyboardAvoidingCollectionView.m @@ -17,6 +17,7 @@ @implementation TPKeyboardAvoidingCollectionView - (void)setup { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TPKeyboardAvoiding_keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TPKeyboardAvoiding_keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TPKeyboardAvoiding_keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; } -(id)initWithFrame:(CGRect)frame { diff --git a/TPKeyboardAvoiding/TPKeyboardAvoidingScrollView.m b/TPKeyboardAvoiding/TPKeyboardAvoidingScrollView.m index 2959ec03..a5a23c28 100644 --- a/TPKeyboardAvoiding/TPKeyboardAvoidingScrollView.m +++ b/TPKeyboardAvoiding/TPKeyboardAvoidingScrollView.m @@ -17,6 +17,7 @@ @implementation TPKeyboardAvoidingScrollView - (void)setup { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TPKeyboardAvoiding_keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TPKeyboardAvoiding_keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TPKeyboardAvoiding_keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; } -(id)initWithFrame:(CGRect)frame { diff --git a/TPKeyboardAvoiding/TPKeyboardAvoidingTableView.m b/TPKeyboardAvoiding/TPKeyboardAvoidingTableView.m index e1b57efa..dcfed357 100644 --- a/TPKeyboardAvoiding/TPKeyboardAvoidingTableView.m +++ b/TPKeyboardAvoiding/TPKeyboardAvoidingTableView.m @@ -17,6 +17,7 @@ @implementation TPKeyboardAvoidingTableView - (void)setup { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TPKeyboardAvoiding_keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TPKeyboardAvoiding_keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TPKeyboardAvoiding_keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; } -(id)initWithFrame:(CGRect)frame { diff --git a/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.h b/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.h index 86c15183..c82dab40 100644 --- a/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.h +++ b/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.h @@ -13,10 +13,11 @@ - (void)TPKeyboardAvoiding_scrollToActiveTextField; - (void)TPKeyboardAvoiding_keyboardWillShow:(NSNotification*)notification; +- (void)TPKeyboardAvoiding_keyboardWillChangeFrame:(NSNotification *)notification; - (void)TPKeyboardAvoiding_keyboardWillHide:(NSNotification*)notification; - (void)TPKeyboardAvoiding_updateContentInset; - (void)TPKeyboardAvoiding_updateFromContentSizeChange; - (void)TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:(UIView*)view; - (UIView*)TPKeyboardAvoiding_findFirstResponderBeneathView:(UIView*)view; --(CGSize)TPKeyboardAvoiding_calculatedContentSizeFromSubviewFrames; +- (CGSize)TPKeyboardAvoiding_calculatedContentSizeFromSubviewFrames; @end diff --git a/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m b/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m index 782a9e96..4dd5d90e 100644 --- a/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m +++ b/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m @@ -48,7 +48,7 @@ - (void)TPKeyboardAvoiding_keyboardWillShow:(NSNotification*)notification { UIView *firstResponder = [self TPKeyboardAvoiding_findFirstResponderBeneathView:self]; - state.keyboardRect = [self convertRect:[[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil]; + state.keyboardRect = [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue]; state.keyboardVisible = YES; state.priorInset = self.contentInset; state.priorScrollIndicatorInsets = self.scrollIndicatorInsets; @@ -83,6 +83,48 @@ - (void)TPKeyboardAvoiding_keyboardWillShow:(NSNotification*)notification { [UIView commitAnimations]; } +- (void)TPKeyboardAvoiding_keyboardWillChangeFrame:(NSNotification *)notification{ + TPKeyboardAvoidingState *state = self.keyboardAvoidingState; + + if ([[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue] > 0.0f ) { + return; + } + + UIView *firstResponder = [self TPKeyboardAvoiding_findFirstResponderBeneathView:self]; + + state.keyboardRect = [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue]; + state.keyboardVisible = YES; + + if ( [self isKindOfClass:[TPKeyboardAvoidingScrollView class]] ) { + state.priorContentSize = self.contentSize; + + if ( CGSizeEqualToSize(self.contentSize, CGSizeZero) ) { + // Set the content size, if it's not set. Do not set content size explicitly if auto-layout + // is being used to manage subviews + self.contentSize = [self TPKeyboardAvoiding_calculatedContentSizeFromSubviewFrames]; + } + } + + // Shrink view's inset by the keyboard's height, and scroll to show the text field/view being edited + [UIView beginAnimations:nil context:NULL]; + [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]]; + [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]]; + + self.contentInset = [self TPKeyboardAvoiding_contentInsetForKeyboard]; + + if ( firstResponder ) { + CGFloat viewableHeight = self.bounds.size.height - self.contentInset.top - self.contentInset.bottom; + [self setContentOffset:CGPointMake(self.contentOffset.x, + [self TPKeyboardAvoiding_idealOffsetForView:firstResponder + withViewingAreaHeight:viewableHeight]) + animated:NO]; + } + + self.scrollIndicatorInsets = self.contentInset; + + [UIView commitAnimations]; +} + - (void)TPKeyboardAvoiding_keyboardWillHide:(NSNotification*)notification { TPKeyboardAvoidingState *state = self.keyboardAvoidingState; @@ -227,7 +269,8 @@ - (UIEdgeInsets)TPKeyboardAvoiding_contentInsetForKeyboard { TPKeyboardAvoidingState *state = self.keyboardAvoidingState; UIEdgeInsets newInset = self.contentInset; CGRect keyboardRect = state.keyboardRect; - newInset.bottom = keyboardRect.size.height - (CGRectGetMaxY(keyboardRect) - CGRectGetMaxY(self.bounds)); + CGRect bounds = [self.superview convertRect:self.frame toView:nil]; + newInset.bottom = keyboardRect.size.height - (CGRectGetMaxY(keyboardRect) - CGRectGetMaxY(bounds)); return newInset; }