Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在UIScrollView上以正确的方式设置ContentOffset_Iphone_Ios_Objective C_Cocoa Touch_Uiscrollview - Fatal编程技术网

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
中,但有时它会从scrollView
contentSize
中滚出,越低,这样就可以滚动。这是因为我在调用上述方法之前修改了
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));