I'm using this code to scroll my UIScrollView
down because I'm adding a new UIView
on it from the bottom and I want to scroll down to it. I do it like this:
CGPoint newOffset = CGPointMake(mainScrollView.contentOffset.x, mainScrollView.contentOffset.y + floorf(bottomAttachmentView.frame.size.height / bottomAttachmentView.multFactor));
[mainScrollView setContentOffset:newOffset animated:YES];
I basically add my new element's height to the y
of UIScrollView
's contentOffset
but sometimes it scrolls out of the scrollView contentSize
, lower, that it is possible to scroll. It happens because I modify the contentSize
before calling the method above and the height of the Scroll View shrinks.
How do you call the setContentOffset
so it wouldn't make my scrollView scroll out of it's own contentSize
? Thanks!
All I had to do actually, was scroll my UIScrollView
to the bottom like this:
CGPoint bottomOffset = CGPointMake(0, [mainScrollView contentSize].height - mainScrollView.frame.size.height);
[mainScrollView setContentOffset:bottomOffset animated:YES];
You can also use this to scroll to bottom of scrollView (Swift 4)
let targetRect = CGRect(x: 0, y: mainScrollView.contentSize.height, width: 1, height: 1)
scrollView.scrollRectToVisible(targetRect, animated: true)
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