文章

ScrollView的ContentSize自适应小技巧

大致意思就是布局要撑开ScrollView,也就是布局到最下面一个元素之后,最下面的元素要触底,将view撑开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
addSubview(scrollView)

scrollView.snp.makeConstraints { make in
    make.top.equalTo(self.snp.top).offset(18)
    make.bottom.equalTo(self.snp.bottom).offset(0)
    make.horizontalEdges.equalTo(self).offset(0)
}

scrollView.addSubView(exampleView)
exampleView.snp.makeConstraints { make in
    make.width.equalTo(265)
    make.height.equalTo(117)
    make.centerX.equalTo(contentView)
    make.top.equalTo(self).inset(500)
}

scrollView.addSubView(button)
button.snp.makeConstraints { make in
    make.centerX.equalToSuperview()
    make.width.equalTo(UIScreen.main.bounds.width - 56)
    make.height.equalTo(300)
                     
    // 要先设置宽高,再设置top bottom,才会让父scrollView的contentSize自适应
    make.top.equalTo(exampleView.snp.bottom).offset(40)
    make.bottom.equalTo(contentView.snp.bottom).offset(-30)
}
本文由作者按照 CC BY 4.0 进行授权