什么是ScrollView的contentOffset和contentInset

Mic*_*oos 31 ios swift

我有2个关于ScrollView的问题.一个是我的主要问题,另一个是次要的混乱.

1.什么到底是contentOffsetcontentInsetScrollView?我将在下面提供更多详情.

我创造了一个ScrollView,但似乎ScrollView无法向任何方向滚动.这是关于抵消的问题吗?还是插图?我将在下面提供更多详情.

问题1的详细信息:

有时contentOffset属性和contentInset属性似乎有很多不同,但是当我尝试实现一些与这两个属性相关的函数时,我感到困惑.我在这里做一个简单的说明.你能帮我弄清楚哪个部分是偏移的,哪个部分是插入的?

在此输入图像描述

问题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")
        }
Run Code Online (Sandbox Code Playgroud)

它无法滚动.它只能包含很少的东西(Buttons或TextFields ......)来填充它的大小,而不是把更多的东西放在我们看不到的地方.

设置初始和插入的问题是什么?我该如何解决此问题?如果我这样做,我怎么能以编程方式将东西(按钮......)添加到未显示的位置?

我希望你能告诉我一些事情!

Rob*_*Rob 52

一些观察:

  • contentOffset是其中用户已经当前滚动视图内滚动.这显然会随着用户滚动而改变.您通常不会以编程方式更改此项(除非您希望以编程方式在某处滚动,例如,使用按钮滚动到顶部).

  • contentInset是多少内容是视觉上的滚动视图内边界(即什么是"边缘"滚动视图中的).您通常在IB或中设置此项一次viewDidLoad,因为滚动视图已实例化.

  • contentSize是滚动内容有多大.请注意,使用autolayout时,您不必手动指定,而是通过在滚动视图及其子视图之间指定的约束(以及子视图的约束和子视图之间的约束)计算.

为了使滚动正常工作,它是(a)bounds滚动视图的组合,减少任何contentInset; (b)contentSize根据子视图的限制为您计算的数据.


Ene*_*nso 8

contentOffset表示滚动视图内容的当前位置,相对于左上角的原点坐标.除非您想以编程方式调整滚动位置,否则不应以编程方式设置此值.

contentInset允许在scrollview中指定内容周围的边距.您可以通过编程方式指定边距,如下所示:

scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 7.0)
Run Code Online (Sandbox Code Playgroud)