ios ScrollView 的 contentOffset 和 contentInset 到底是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33286574/
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
What exactly are contentOffset& contentInset of ScrollView
提问by Microos
I have 2 questions about ScrollView. One is my major question, Another one is secondary confusion.
我有 2 个关于 ScrollView 的问题。一个是我的主要问题,另一个是次要的混淆。
1.What exactly are contentOffset
and contentInset
of ScrollView
? I will give more Details below.
1.什么到底是contentOffset
和contentInset
的ScrollView
?我将在下面提供更多详细信息。
2.I create a ScrollView
, but it seems the ScrollView
can't roll in any direction.Is the problem about offset? Or inset? I will give more Details below.
2.我创建了一个ScrollView
,但似乎ScrollView
无法向任何方向滚动。是偏移量的问题吗?还是插入?我将在下面提供更多详细信息。
Details for question 1:
问题 1 的详细信息:
Sometimes contentOffset
property and contentInset
property seem to have lots of difference, but when I try to implement some of functions that relate to those two properties, I get confused.
And I make a simple illustration here. Can you help me figure it out that which part is offset and which part is inset?
有时contentOffset
property 和contentInset
property 似乎有很多不同,但是当我尝试实现一些与这两个属性相关的功能时,我感到困惑。我在这里做一个简单的说明。你能帮我弄清楚哪个部分是偏移的,哪个部分是插入的吗?
Details for question 2:
问题 2 的详细信息:
I use following function to add a ScrollView
我使用以下函数添加一个 ScrollView
func addScrollView(){
// assign a new ScrollView to "scroll" class member
scroll = UIScrollView(frame: CGRectZero)
scroll!.translatesAutoresizingMaskIntoConstraints = false
// change it to gray color to help recognising
scroll!.backgroundColor = UIColor.grayColor()
self.view.addSubview(scroll!)
//add some constraints,[you can neglect following part]
let x = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
self.view.addConstraint(x)
let y = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1/2, constant: 25)
self.view.addConstraint(y)
let left = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1, constant: 10)
self.view.addConstraint(left)
let upperSpacing = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 50)
self.view.addConstraint(upperSpacing)
let w = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 10)
self.view.addConstraint(w)
let h = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: scroll!, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0)
self.view.addConstraint(h)
print("add scroll view Successed")
}
And it is not able to roll. It can only contain few things(Buttons or TextFields...) to fill its size, rather than put more things at places where we can't see.
它无法滚动。它只能包含很少的东西(按钮或文本字段...)来填充它的大小,而不是在我们看不到的地方放置更多的东西。
So is the problem about setting outset & inset ? How can I set to fix this? And If I do so, how can I add things(buttons..) to the un-shown places programatically?
那么是关于设置开始和插入的问题吗?我该如何设置来解决这个问题?如果我这样做,我如何以编程方式将东西(按钮..)添加到未显示的地方?
I hope you can tell me some things!
我希望你能告诉我一些事情!
回答by Rob
A few observations:
一些观察:
The
contentOffset
is where the user has currently scrolled within the scroll view. This obviously changes as the user scrolls. You don't generally change this programmatically (unless you want to programmatically scroll somewhere, e.g. have a button to scroll to the top).The
contentInset
is how much the content is visually inset inside the scroll view (i.e. what the "margins" within the scrollview are). You generally set this once in IB or inviewDidLoad
, as the scroll view is instantiated.The
contentSize
is how big the scrollable content is. Note, with autolayout, you don't have to specify this manually, but rather is calculated by the constraints that you specified between the scroll view and its subviews (and the constraints for the subviews and between the subviews).
这
contentOffset
是用户当前在滚动视图中滚动的位置。这显然会随着用户滚动而改变。您通常不会以编程方式更改此设置(除非您想以编程方式滚动某处,例如有一个滚动到顶部的按钮)。这
contentInset
是滚动视图中视觉插入的内容量(即滚动视图中的“边距”是多少)。您通常在 IB 或 in 中设置一次viewDidLoad
,因为滚动视图已实例化。该
contentSize
是滚动内容有多大。请注意,使用自动布局,您不必手动指定它,而是由您在滚动视图及其子视图之间指定的约束(以及子视图和子视图之间的约束)计算。
To get scrolling to work correctly, it is a combination of (a) the bounds
of the scroll view, less any contentInset
; and (b) the contentSize
, as calculated for you from the constraints of the subviews.
为了使滚动正常工作,它是 (a)bounds
滚动视图的组合,减去任何contentInset
; 和 (b) contentSize
,根据子视图的约束为您计算。
回答by Eneko Alonso
contentOffset
indicates the current position of the scroll view content, relative to the origin coordinates on the top-left corner. You shouldn't programmatically set this value unless you would like to programmatically adjust the scroll position.
contentOffset
指示滚动视图内容的当前位置,相对于左上角的原点坐标。除非您想以编程方式调整滚动位置,否则不应以编程方式设置此值。
contentInset
allows to specify margins around the content in the scrollview. You can specify the margin programmatically as follows:
contentInset
允许在滚动视图中指定内容周围的边距。您可以按如下方式以编程方式指定边距:
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 7.0)