xcode UIScrollView 如何设置 ContentInset 和 ScrollerInset

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11934452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:13:38  来源:igfitidea点击:

UIScrollView how to set ContentInset and ScrollerInset

iosxcodeuiscrollviewstoryboard

提问by Maik639

im not able to configure my UIScrollView.

我无法配置我的 UIScrollView。

enter image description here

在此处输入图片说明

The image above shows my view. The orange box is my UIScrollView and the blue boxes (plus the orange box) is the view that i want to scroll.

上图显示了我的观点。橙色框是我的 UIScrollView,蓝色框(加上橙色框)是我想要滚动的视图。

The View is 520px with and the UIScollView (as usual) 320px.

视图是 520 像素,而 UIScollView(像往常一样)是 320 像素。

How have I to set the ContentInset and ScollerInset?

我该如何设置 ContentInset 和 ScollerInset?

采纳答案by Ian L

I'm not sure exactly what your question is, but I'm guessing your scrollview is not scrolling as expected.

我不确定您的问题到底是什么,但我猜您的滚动视图没有按预期滚动。

You need to set the contentSize property to tell the scrollview that there is more content than just the viewable area (e.g. myScrollView.contentSize = CGSizeMake(520.0, 100.0);)

您需要设置 contentSize 属性来告诉滚动视图除了可视区域之外还有更多内容(例如myScrollView.contentSize = CGSizeMake(520.0, 100.0);

If you want to start with the content centred as per your diagram, you can to set the contentOffset property. (e.g. myScrollView.contentOffset = CGPointMake((myScrollView.contentSize.width - myScrollView.frame.size.width) / 2, 0.0);)

如果您想从按照图表居中的内容开始,您可以设置 contentOffset 属性。(例如myScrollView.contentOffset = CGPointMake((myScrollView.contentSize.width - myScrollView.frame.size.width) / 2, 0.0);

回答by dasblinkenlight

Since the heights of the scroll view and its content match, and because it does not appear from your picture that you would like to add an empty space to the content, you should keep the contentInsetset to zero (as it is by default). If you would like the content's middle to be visible through the scroll view, set content offset to (520-320)/2 = 100, like this:

由于滚动视图的高度与其内容匹配,并且因为从您的图片中没有显示您希望向内容添加空白空间,您应该将contentInset设置保持为零(默认情况下)。如果您希望内容的中间通过滚动视图可见,请将内容偏移设置为(520-320)/2 = 100,如下所示:

[scrollView setContentOffset:CGPointMake(100, 0) animated:YES];
// If you do not want to see the view scroll, change this ^^^ to NO