Skip to content

Commit

Permalink
iOS 11 support;
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyf authored and michaeltyson committed Nov 8, 2017
1 parent 487627d commit 8738417
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,17 @@ -(CGFloat)TPKeyboardAvoiding_idealOffsetForView:(UIView *)view withViewingAreaHe
CGRect subviewRect = [view convertRect:view.bounds toView:self];

__block CGFloat padding = 0.0;
__block UIEdgeInsets contentInset;

#ifdef __IPHONE_11_0
if (@available(iOS 11.0, *)) {
contentInset = self.adjustedContentInset;
} else {
contentInset = self.contentInset;
}
#else
contentInset = self.contentInset;
#endif

void(^centerViewInViewableArea)() = ^ {
// Attempt to center the subview in the visible space
Expand All @@ -364,7 +375,7 @@ -(CGFloat)TPKeyboardAvoiding_idealOffsetForView:(UIView *)view withViewingAreaHe
// Ideal offset places the subview rectangle origin "padding" points from the top of the scrollview.
// If there is a top contentInset, also compensate for this so that subviewRect will not be placed under
// things like navigation bars.
offset = subviewRect.origin.y - padding - self.contentInset.top;
offset = subviewRect.origin.y - padding - contentInset.top;
};

// If possible, center the caret in the visible space. Otherwise, center the entire view in the visible space.
Expand All @@ -387,7 +398,7 @@ -(CGFloat)TPKeyboardAvoiding_idealOffsetForView:(UIView *)view withViewingAreaHe
// Ideal offset places the subview rectangle origin "padding" points from the top of the scrollview.
// If there is a top contentInset, also compensate for this so that subviewRect will not be placed under
// things like navigation bars.
offset = caretRect.origin.y - padding - self.contentInset.top;
offset = caretRect.origin.y - padding - contentInset.top;
} else {
centerViewInViewableArea();
}
Expand All @@ -397,14 +408,14 @@ -(CGFloat)TPKeyboardAvoiding_idealOffsetForView:(UIView *)view withViewingAreaHe

// Constrain the new contentOffset so we can't scroll past the bottom. Note that we don't take the bottom
// inset into account, as this is manipulated to make space for the keyboard.
CGFloat maxOffset = contentSize.height - viewAreaHeight - self.contentInset.top;
CGFloat maxOffset = contentSize.height - viewAreaHeight - contentInset.top;
if (offset > maxOffset) {
offset = maxOffset;
}

// Constrain the new contentOffset so we can't scroll past the top, taking contentInsets into account
if ( offset < -self.contentInset.top ) {
offset = -self.contentInset.top;
if ( offset < -contentInset.top ) {
offset = -contentInset.top;
}

return offset;
Expand Down

0 comments on commit 8738417

Please sign in to comment.