setContentOffset on UIScrollView the right way

有些话、适合烂在心里 提交于 2019-12-10 20:27:51

问题


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!


回答1:


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];



回答2:


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)


来源:https://stackoverflow.com/questions/15159023/setcontentoffset-on-uiscrollview-the-right-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!