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 a new TPKeyboardAvoiding_setAdditionalContentOffset to UIScrollView. #167

Open
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@
- (void)TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView:(UIView*)view;
- (UIView*)TPKeyboardAvoiding_findFirstResponderBeneathView:(UIView*)view;
-(CGSize)TPKeyboardAvoiding_calculatedContentSizeFromSubviewFrames;

/// When set, apply an additional content offset to the offset
/// TPKeyboardAvoiding would apply normally.
- (void)TPKeyboardAvoiding_setAdditionalContentOffset:(CGPoint)contentOffset;

@end
17 changes: 13 additions & 4 deletions TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ @interface TPKeyboardAvoidingState : NSObject
@property (nonatomic, assign) BOOL keyboardVisible;
@property (nonatomic, assign) CGRect keyboardRect;
@property (nonatomic, assign) CGSize priorContentSize;
@property (nonatomic, assign) CGPoint additionalContentOffset;


@property (nonatomic) BOOL priorPagingEnabled;
Expand Down Expand Up @@ -88,9 +89,10 @@ - (void)TPKeyboardAvoiding_keyboardWillShow:(NSNotification*)notification {
self.contentInset = [self TPKeyboardAvoiding_contentInsetForKeyboard];

CGFloat viewableHeight = self.bounds.size.height - self.contentInset.top - self.contentInset.bottom;
[self setContentOffset:CGPointMake(self.contentOffset.x,
[self setContentOffset:CGPointMake(self.contentOffset.x + state.additionalContentOffset.x,
[self TPKeyboardAvoiding_idealOffsetForView:firstResponder
withViewingAreaHeight:viewableHeight])
withViewingAreaHeight:viewableHeight]
+ state.additionalContentOffset.y)
animated:NO];

self.scrollIndicatorInsets = self.contentInset;
Expand Down Expand Up @@ -172,8 +174,10 @@ -(void)TPKeyboardAvoiding_scrollToActiveTextField {

CGFloat visibleSpace = self.bounds.size.height - self.contentInset.top - self.contentInset.bottom;

CGPoint idealOffset = CGPointMake(0, [self TPKeyboardAvoiding_idealOffsetForView:[self TPKeyboardAvoiding_findFirstResponderBeneathView:self]
withViewingAreaHeight:visibleSpace]);
CGPoint idealOffset = CGPointMake(0 + state.additionalContentOffset.x,
[self TPKeyboardAvoiding_idealOffsetForView:[self TPKeyboardAvoiding_findFirstResponderBeneathView:self]
withViewingAreaHeight:visibleSpace]
+ state.additionalContentOffset.y);

// 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
Expand All @@ -182,6 +186,11 @@ -(void)TPKeyboardAvoiding_scrollToActiveTextField {
});
}

- (void)TPKeyboardAvoiding_setAdditionalContentOffset:(CGPoint)contentOffset {
TPKeyboardAvoidingState *state = self.keyboardAvoidingState;
state.additionalContentOffset = contentOffset;
}

#pragma mark - Helpers

- (UIView*)TPKeyboardAvoiding_findFirstResponderBeneathView:(UIView*)view {
Expand Down