I added very big text to UItextView. My initial offset is -55. Then I scrolled to the bottom of UITextView. My offset is 406.
Then I called scrollToZero. My offset is -55. I called scrollToZero again and my offset is 0. Why is scrollToZero so unpredictable? I don't undestand why offset changed when I clicked again.
-(void) viewDidLoad
{
[super viewDidLoad];
textView.text = @"Very big text";
textView.contentInset = UIEdgeInsetsMake(55.0, 0, 0, 0);
[textView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
}
-(IBAction) scrollToZero:(id)sender
{
[textView scrollRectToVisible:CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height) animated:NO];
}
-(IBAction) onLog:(id)sender
{
NSLog(@"___content offset %f", textView.contentOffset.y);
}
I have been battling this very problem. I am convinced that this is a bug in the UIScrollView class, I can see no other explanation.
First set your insets to zero, call scrollRectToVisible:animated:, and then restore your insets. It only matters if the scroll-to rect is 'left of' the current rect. 'right of' works as expected.
CGRect rect = self.scrollView.bounds;
CGRect scrollToRect = CGRectOffset(rect, scrollDelta, 0);
if (CGRectIsLeftOfRect(scrollToRect, rect)) {
UIEdgeInsets insets = self.carouselView.contentInset;
self.scrollView.contentInset = UIEdgeInsetsZero;
[self.scrollView scrollRectToVisible:scrollToRect animated:animated];
self.scrollView.contentInset = insets;
} else {
[self.scrollView scrollRectToVisible:scrollToRect animated:animated];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With