I do not fully understand scrollRectToVisible when I use contentInset

I added a very large text to the UItextView. My initial offset is -55. Then I scrolled to the end of the 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 do not understand why the bias changed when I pressed 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); } 
+7
source share
1 answer

I struggled with this very problem. I am convinced that this is a mistake in the UIScrollView class, I see no other explanation.

First set your inserts to zero, call scrollRectToVisible: animated :, and then restore your inserts. This only matters if the direct scrolling is โ€œto the leftโ€ of the current rectangle. "right to" 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]; } 
+8
source

All Articles