Skip to content

Commit

Permalink
Merge pull request #243 from RunesReader/master
Browse files Browse the repository at this point in the history
Update UIScrollView+TPKeyboardAvoidingAdditions.m
  • Loading branch information
michaeltyson authored May 18, 2017
2 parents c30c491 + 68b0e4d commit 5e2f244
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
static const CGFloat kCalculatedContentPadding = 10;
static const CGFloat kMinimumScrollOffsetPadding = 20;

static NSString * const kUIKeyboardAnimationDurationUserInfoKey = @"UIKeyboardAnimationDurationUserInfoKey";

static const int kStateKey;

#define _UIKeyboardFrameEndUserInfoKey (&UIKeyboardFrameEndUserInfoKey != NULL ? UIKeyboardFrameEndUserInfoKey : @"UIKeyboardBoundsUserInfoKey")
Expand All @@ -26,6 +28,7 @@ @interface TPKeyboardAvoidingState : NSObject
@property (nonatomic, assign) BOOL priorPagingEnabled;
@property (nonatomic, assign) BOOL ignoringNotifications;
@property (nonatomic, assign) BOOL keyboardAnimationInProgress;
@property (nonatomic, assign) CGFloat animationDuration;
@end

@implementation UIScrollView (TPKeyboardAvoidingAdditions)
Expand All @@ -43,14 +46,16 @@ - (TPKeyboardAvoidingState*)keyboardAvoidingState {
}

- (void)TPKeyboardAvoiding_keyboardWillShow:(NSNotification*)notification {
NSDictionary *info = [notification userInfo];
TPKeyboardAvoidingState *state = self.keyboardAvoidingState;

state.animationDuration = [[info objectForKey:kUIKeyboardAnimationDurationUserInfoKey] doubleValue];

CGRect keyboardRect = [self convertRect:[[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
CGRect keyboardRect = [self convertRect:[[info objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
if (CGRectIsEmpty(keyboardRect)) {
return;
}

TPKeyboardAvoidingState *state = self.keyboardAvoidingState;


if ( state.ignoringNotifications ) {
return;
}
Expand Down Expand Up @@ -217,11 +222,11 @@ -(void)TPKeyboardAvoiding_scrollToActiveTextField {

// Ordinarily we'd use -setContentOffset:animated:YES here, but it interferes with UIScrollView
// behavior which automatically ensures that the first responder is within its bounds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self setContentOffset:idealOffset animated:YES];
[UIView animateWithDuration:state.animationDuration animations:^{
self.contentOffset = idealOffset;
} completion:^(BOOL finished) {
state.ignoringNotifications = NO;
});
}];
}

#pragma mark - Helpers
Expand Down

0 comments on commit 5e2f244

Please sign in to comment.