From 7b2143a054c81c63cbb140725d909bf858084811 Mon Sep 17 00:00:00 2001 From: Marc Fleutiaux Date: Thu, 26 Oct 2017 15:59:51 +0200 Subject: [PATCH] Using scrollRectToVisible:animated: seems to cause some problems to adjust scrollview offset with the keyboard. --- Framework/Sources/View/UIScrollView+HLSExtensions.m | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Framework/Sources/View/UIScrollView+HLSExtensions.m b/Framework/Sources/View/UIScrollView+HLSExtensions.m index 13e792f54..a81e52885 100644 --- a/Framework/Sources/View/UIScrollView+HLSExtensions.m +++ b/Framework/Sources/View/UIScrollView+HLSExtensions.m @@ -242,7 +242,7 @@ - (void)adjustOffsetWithKeyboardEndFrameInWindow:(CGRect)keyboardEndFrameInWindo // Adjust content. Adjust depending on the space available vertically (398 is the horizontal keyboard height on a standard reference iPad) self.contentInset = UIEdgeInsetsMake(self.contentInset.top, self.contentInset.left, - keyboardHeightAdjustment + keyboardDistance * (CGRectGetHeight([UIScreen mainScreen].bounds) - CGRectGetHeight(keyboardEndFrameInWindow)) / (768.f - 398.f), + keyboardHeightAdjustment + keyboardDistance, self.contentInset.right); self.scrollIndicatorInsets = UIEdgeInsetsMake(self.scrollIndicatorInsets.top, self.scrollIndicatorInsets.left, @@ -251,9 +251,16 @@ - (void)adjustOffsetWithKeyboardEndFrameInWindow:(CGRect)keyboardEndFrameInWindo [s_adjustedScrollViews addObject:self]; void (^scrollBlock)(CGRect) = ^(CGRect focusRect) { + CGRect focusRectInScrollView = [self convertRect:focusRect fromView:firstResponderView]; + CGFloat focusViewYPlusHeight = focusRectInScrollView.origin.y + focusRectInScrollView.size.height; + CGFloat keyboardExpandPosition = CGRectGetHeight(self.bounds) - keyboardHeightAdjustment - keyboardDistance; [UIView animateWithDuration:0.25 animations:^{ - CGRect focusRectInScrollView = [self convertRect:focusRect fromView:firstResponderView]; - [self scrollRectToVisible:focusRectInScrollView animated:NO]; + if (focusViewYPlusHeight > keyboardExpandPosition) { + [self setContentOffset:CGPointMake(0.f, focusViewYPlusHeight - keyboardExpandPosition) animated:NO]; + } + else { + [self setContentOffset:CGPointMake(0.f, -(keyboardExpandPosition - focusViewYPlusHeight)) animated:NO]; + } }]; };