我尝试创建以下代码,其中两个方法都在viewDidLoad
中调用,但是没有添加contentView。
func setupScrollView() {
//Initialize scrollView
scrollView = UIScrollView(frame: self.view.bounds)
self.view.addSubview(scrollView)
//Customize tableView
scrollView.backgroundColor = UIColor.red
//set contentSize
self.scrollView.contentSize = self.view.bounds.size
scrollView.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
//setup contentView
self.contentView = UIView()
self.scrollView.addSubview(self.contentView)
//Customize contentView
self.contentView.backgroundColor = UIColor.green
self.scrollView.addSubview(self.contentView)
self.view.addSubview(self.scrollView)
}
func setupConstraints() {
//tableView
contentView.snp.makeConstraints({ make in
make.left.equalTo(0)
make.right.equalTo(0)
make.top.equalTo(0)
make.bottom.equalTo(0)
})
}