Iphone 在UIScrollView上以正确的方式设置ContentOffset
我正在使用此代码向下滚动我的Iphone 在UIScrollView上以正确的方式设置ContentOffset,iphone,ios,objective-c,cocoa-touch,uiscrollview,Iphone,Ios,Objective C,Cocoa Touch,Uiscrollview,我正在使用此代码向下滚动我的UIScrollView,因为我正在从底部添加一个新的UIView,我想向下滚动到它。我是这样做的: CGPoint newOffset = CGPointMake(mainScrollView.contentOffset.x, mainScrollView.contentOffset.y + floorf(bottomAttachmentView.frame.size.height / bottomAttachmentView.multFactor)); [main
UIScrollView
,因为我正在从底部添加一个新的UIView
,我想向下滚动到它。我是这样做的:
CGPoint newOffset = CGPointMake(mainScrollView.contentOffset.x, mainScrollView.contentOffset.y + floorf(bottomAttachmentView.frame.size.height / bottomAttachmentView.multFactor));
[mainScrollView setContentOffset:newOffset animated:YES];
CGPoint bottomOffset = CGPointMake(0, [mainScrollView contentSize].height - mainScrollView.frame.size.height);
[mainScrollView setContentOffset:bottomOffset animated:YES];
基本上,我将新元素的高度添加到UIScrollView
的y
的contentOffset
中,但有时它会从scrollViewcontentSize
中滚出,越低,这样就可以滚动。这是因为我在调用上述方法之前修改了contentSize
,并且滚动视图的高度缩小了
如何调用
setContentOffset
,使我的scrollView不会从它自己的contentSize
中滚动出来?谢谢 实际上,我所要做的就是将我的UIScrollView
滚动到底,如下所示:
CGPoint newOffset = CGPointMake(mainScrollView.contentOffset.x, mainScrollView.contentOffset.y + floorf(bottomAttachmentView.frame.size.height / bottomAttachmentView.multFactor));
[mainScrollView setContentOffset:newOffset animated:YES];
CGPoint bottomOffset = CGPointMake(0, [mainScrollView contentSize].height - mainScrollView.frame.size.height);
[mainScrollView setContentOffset:bottomOffset animated:YES];
您还可以使用此选项滚动到scrollView(Swift 4)的底部
你缩小mainScrollView,然后将bottomAttachmentView添加到mainScrollView,并想滚动到它?@Rikkles是的,但问题是它只占用
scrollView
底部30%的部分,当我尝试使用这个函数时,它只是从scrollView
中滚动出来,这个解决方案会更干净一些,如果还要考虑底部内容插入:CGPoint bottomOffset=CGPointMake(self.scrollView.contentOffset.x,(self.scrollView.contentSize.height-self.scrollView.frame.size.height+self.scrollView.contentInset.bottom));