Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for 'keyboardWillChangeFrame' #97

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TPKeyboardAvoiding/TPKeyboardAvoidingCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions TPKeyboardAvoiding/TPKeyboardAvoidingScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions TPKeyboardAvoiding/TPKeyboardAvoidingTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 45 additions & 2 deletions TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down