Swift 在UIScrollView中使用snapkit/autolayout

Swift 在UIScrollView中使用snapkit/autolayout,swift,autolayout,swift4,ios-autolayout,snapkit,Swift,Autolayout,Swift4,Ios Autolayout,Snapkit,我有一个应用程序,将有一个 滚动视图 内容视图 图表 钮扣 其他东西 我试着约束它,如下所示,但我缺少一个约束,我无法找到我需要什么 self.view.addSubview(self.scrollView) self.scrollView.snp.makeConstraints { (make) in make.edges.equalTo(self.view) } let contentView = UIView() sel

我有一个应用程序,将有一个

  • 滚动视图
    • 内容视图
      • 图表
      • 钮扣
      • 其他东西
我试着约束它,如下所示,但我缺少一个约束,我无法找到我需要什么

    self.view.addSubview(self.scrollView)
    self.scrollView.snp.makeConstraints { (make) in
        make.edges.equalTo(self.view)
    }
    let contentView = UIView()

    self.scrollView.addSubview(contentView)
    contentView.snp.makeConstraints { (make) in
        make.top.bottom.equalTo(self.scrollView)
        make.left.right.equalTo(self.view)
    }
    contentView.addSubview(self.chart)
    self.chart.snp.makeConstraints { (make) in
        // http://snapkit.io/docs/
        make.edges.equalTo(contentView).inset(UIEdgeInsets(top: 30, left: 0, bottom: 50, right: 0))
    }

其中,您需要为contentView添加宽度/高度或对齐约束。试试这个:

contentView.snp.makeConstraints { (make) in
    make.top.bottom.equalTo(self.scrollView)
    make.left.right.equalTo(self.view)
    make.width.equalTo(self.scrollView)
    make.height.equalTo(self.scrollView)
    // or:
    // make.centerX.equalTo(self.scrollView)
    // make.centerY.equalTo(self.scrollView)
}

@简言之,伊利亚·哈拉贝的回答是:

contentView.snp.makeConstraints { (make) in
    make.left.right.equalTo(self.view)
    make.width.height.top.bottom.equalTo(self.scrollView)
}
附言:


通常我们使用
make.leading.training
,而不是
make.left.right
,来支持RTL。

谢谢。如何根据
contentView
设置scrollView内容高度?内容高度将自动计算