iosswiftautolayoutscrollviewsnapkit

Create ScrollView programmatically using Snapkit


I'm working on making scrollview programmatically using snapkit, it works scroll but the problem is that the last element(systemTeal color tempView) can't see fully.

I create contentView inside Scrollview, which is containing every element.

class ScrollViewController: UIViewController {

    let scrollView = UIScrollView()
    let contentView = UIView()

    private var datePickerView = UIView().then {
        $0.backgroundColor = .pastelGreen
        $0.layer.cornerRadius = 8
    }    
   
    var colorGraphView = UIView().then {
        $0.backgroundColor = .clear
    }
    
    var tableView = UITableView().then {
        $0.backgroundColor = .shadowColor
    }

    var tempView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        commonInit()
        scrollView.delegate = self
    }
    
    func commonInit() {
        self.view.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.edges.equalTo(self.view.safeAreaLayoutGuide)
        }
        
        scrollView.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.edges.equalTo(self.scrollView)
            make.width.equalTo(self.scrollView)
        }

        contentView.addSubview(datePickerView)
        contentView.addSubview(colorGraphView)
        contentView.addSubview(tableView)
        contentView.addSubview(tempView)
        
        datePickerView.snp.makeConstraints { make in
            make.leading.equalTo(contentView.snp.leading).offset(16)
            make.trailing.equalTo(contentView.snp.trailing).offset(-16)
            make.top.equalTo(contentView).offset(26)
            make.height.equalTo(42)
        }

        colorGraphView.snp.makeConstraints { make in
            make.leading.equalTo(contentView).offset(16)
            make.trailing.equalTo(contentView).offset(-16)
            make.top.equalTo(datePickerView.snp.bottom).offset(19)
            make.height.equalTo(390)
        }
        tableView.snp.makeConstraints { make in
            make.leading.equalTo(contentView).offset(16)
            make.trailing.equalTo(contentView).offset(-16)
            make.top.equalTo(colorGraphView.snp.bottom).offset(10)
            make.height.equalTo(150)
        }
        tempView.snp.makeConstraints { make in
            make.top.equalTo(tableView.snp.bottom).offset(10)
            make.leading.equalTo(contentView.snp.leading).offset(16)
            make.trailing.equalTo(contentView.snp.trailing).offset(-16)
            make.height.equalTo(100)
            make.bottom.equalTo(contentView.snp.bottom).offset(-20)
        }

        scrollView.backgroundColor = .brown
        contentView.backgroundColor = .blue
        colorGraphView.backgroundColor = .yellow
        tableView.backgroundColor = .gray
        tempView.backgroundColor = .systemTeal    
}

How create autolayout scrollview without setting the contentSize?

Srcollview looks like this now
scollview looks like this now


Solution

  • I think the problem might be the use of make.edges.equalTo. It'll statically set the edges of the superview to your scrollview and content view, not constraining top, leading, trailing, bottom anchor to the superview. I suggest you to try with 'make.top.leading.trailing.bottom.equalToSuperview()' instead.