关于ScrollView,我有两个问题。第一个是我的主要问题,另一个是次要的困惑。
1.ScrollView
的contentOffset
和contentInset
到底是什么?我会在下面给出更多细节。
2.我创建了一个ScrollView
,但似乎ScrollView
无法在任何方向上滚动。这个问题是关于offset还是inset?我会在下面给出更多细节。
问题1的细节:
有时候,contentOffset
属性和contentInset
属性似乎有很大的区别,但是当我尝试实现一些与这两个属性相关的功能时,我感到困惑。
我在这里做了一个简单的说明图。你能帮我弄清楚哪部分是offset,哪部分是inset吗?
问题2的细节:
我使用以下函数添加一个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")
}
它不能滚动,只能容纳一些东西(按钮或文本字段等)来填充其大小,而不是将更多东西放在我们看不到的地方。
那么问题是否与设置 outset 和 inset 有关?我该如何设置才能解决这个问题?如果这样做,我如何通过编程向未显示的位置添加东西(按钮等)?
希望您能告诉我一些信息!
contentSize
将被计算并设置。因此,假设您的滚动视图高度为480个点,但其所有子视图及其间距的高度(由约束规定)总共为700个点(contentSize.height
为700),则您将能够在480个点的滚动视图内上下滚动那700个点的内容。 - Rob