将 snapkit / autolayout 与 UIScrollView 一起使用
Using snapkit / autolayout with UIScrollView
我有一个应用程序
- 滚动视图
- 内容视图
- 图表
- 按钮
- 其他
我试过如下所示对其进行约束,但我缺少一个约束,我找不到我需要的东西。
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))
}
其中 scrollView = UIScrollView()
您需要为 contentView 添加 width/height 或对齐约束。试试这个:
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)
}
@Ilya Kharabet 的简短回答:
contentView.snp.makeConstraints { (make) in
make.left.right.equalTo(self.view)
make.width.height.top.bottom.equalTo(self.scrollView)
}
PS:
通常我们使用 make.leading.trailing
,而不是 make.left.right
,用于 RTL 支持。
我有一个应用程序
- 滚动视图
- 内容视图
- 图表
- 按钮
- 其他
- 内容视图
我试过如下所示对其进行约束,但我缺少一个约束,我找不到我需要的东西。
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))
}
其中 scrollView = UIScrollView()
您需要为 contentView 添加 width/height 或对齐约束。试试这个:
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)
}
@Ilya Kharabet 的简短回答:
contentView.snp.makeConstraints { (make) in
make.left.right.equalTo(self.view)
make.width.height.top.bottom.equalTo(self.scrollView)
}
PS:
通常我们使用 make.leading.trailing
,而不是 make.left.right
,用于 RTL 支持。