Skip to content

Animated change of contentOffset for case with changing keyboard height #178

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ - (TPKeyboardAvoidingState*)keyboardAvoidingState {

- (void)TPKeyboardAvoiding_keyboardWillShow:(NSNotification*)notification {
CGRect keyboardRect = [self convertRect:[[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
if (CGRectIsEmpty(keyboardRect)) {
if (CGRectIsEmpty(keyboardRect) || !CGRectIntersectsRect(keyboardRect, self.bounds)) {
return;
}

Expand Down Expand Up @@ -79,22 +79,27 @@ - (void)TPKeyboardAvoiding_keyboardWillShow:(NSNotification*)notification {
self.contentSize = [self TPKeyboardAvoiding_calculatedContentSizeFromSubviewFrames];
}
}

int curve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
float duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
if (curve == 0)
curve = 7;
if (duration == 0)
duration = 0.25;
// 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]];
[UIView setAnimationCurve:curve];
[UIView setAnimationDuration:duration];

self.contentInset = [self TPKeyboardAvoiding_contentInsetForKeyboard];

self.scrollIndicatorInsets = self.contentInset;
[self layoutIfNeeded];
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;
[self layoutIfNeeded];

[UIView commitAnimations];
}
Expand Down
2 changes: 2 additions & 0 deletions TPKeyboardAvoidingSample/TPKAScrollViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ - (void)viewDidLoad {
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.placeholder = [NSString stringWithFormat:@"Field %d", i];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.secureTextEntry = i == 39;

[self.scrollView addSubview:textField];

[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:30]];
Expand Down