From d513f1f71f8581dc2c68a30946a4332d45c6ba68 Mon Sep 17 00:00:00 2001 From: okohtenko Date: Wed, 19 Aug 2015 15:18:33 +0300 Subject: [PATCH 1/2] Animated change of contentOffset for case with changing keyboard height --- .../UIScrollView+TPKeyboardAvoidingAdditions.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m b/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m index 614ab6c6..154b979c 100644 --- a/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m +++ b/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m @@ -87,14 +87,14 @@ - (void)TPKeyboardAvoiding_keyboardWillShow:(NSNotification*)notification { 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]; + animated:YES]; - self.scrollIndicatorInsets = self.contentInset; - [self layoutIfNeeded]; [UIView commitAnimations]; } From 206c30238191f6dd97291de3426446626dc78390 Mon Sep 17 00:00:00 2001 From: okohtenko Date: Thu, 20 Aug 2015 10:21:12 +0300 Subject: [PATCH 2/2] Bounce fix --- .../UIScrollView+TPKeyboardAvoidingAdditions.m | 15 ++++++++++----- .../TPKAScrollViewController.m | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m b/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m index 154b979c..fe199fcd 100644 --- a/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m +++ b/TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m @@ -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; } @@ -79,11 +79,16 @@ - (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]; @@ -93,7 +98,7 @@ - (void)TPKeyboardAvoiding_keyboardWillShow:(NSNotification*)notification { [self setContentOffset:CGPointMake(self.contentOffset.x, [self TPKeyboardAvoiding_idealOffsetForView:firstResponder withViewingAreaHeight:viewableHeight]) - animated:YES]; + animated:NO]; [UIView commitAnimations]; diff --git a/TPKeyboardAvoidingSample/TPKAScrollViewController.m b/TPKeyboardAvoidingSample/TPKAScrollViewController.m index 4c3fb845..a7a18fc8 100644 --- a/TPKeyboardAvoidingSample/TPKAScrollViewController.m +++ b/TPKeyboardAvoidingSample/TPKAScrollViewController.m @@ -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]];