@@ -13,7 +13,7 @@ let ScreenWidth = UIScreen.mainScreen().bounds.width
1313let ScreenHeight = UIScreen . mainScreen ( ) . bounds. height
1414let topScrollHeight : CGFloat = UIScreen . mainScreen ( ) . bounds. height / 3
1515let boxWidth : CGFloat = ScreenWidth * 2 / 3
16- let boxGap : CGFloat = 50
16+ let boxGap : CGFloat = 20
1717
1818class ViewController : UIViewController {
1919
@@ -35,6 +35,7 @@ class ViewController: UIViewController {
3535 */
3636 layoutWithContainer ( )
3737// layoutWithAbsoluteView()
38+ // layoutWithCustomePageSize()
3839 }
3940
4041 override func didReceiveMemoryWarning( ) {
@@ -150,6 +151,69 @@ extension ViewController {
150151 }
151152}
152153
154+ // MARK: - 用Container实现自动布局
155+ extension ViewController {
156+ /**
157+ The key is to set clipsToBounds to false and make the width of frame of scrollview less than the width of screen.
158+ Usually the width now is padding + subviewWidth
159+ 关键在于clipsToBounds设置为no,scrollview自身的width小于屏幕宽度,一般设置为padding + 子视图width
160+ */
161+ func layoutWithCustomePageSize( ) {
162+ scrollView. bounces = false
163+ view. addSubview ( scrollView)
164+ scrollView. pagingEnabled = true
165+ scrollView. clipsToBounds = false // *important!* //
166+ scrollView. backgroundColor = UIColor . yellowColor ( )
167+ scrollView. addSubview ( containerView)
168+
169+ containerView. backgroundColor = scrollView. backgroundColor
170+
171+ /**
172+ * 对scrollView添加约束
173+ * Add constraints to scrollView
174+ */
175+ scrollView. snp_makeConstraints { ( make) -> Void in
176+ make. center. equalTo ( view. snp_center)
177+ make. width. equalTo ( boxWidth + boxGap) // *important!* //
178+ make. height. equalTo ( topScrollHeight)
179+ }
180+
181+ /**
182+ * 对containerView添加约束,接下来只要确定containerView的宽度即可
183+ * Add constraints to containerView, the only thing we will do
184+ * is to define the width of containerView
185+ */
186+ containerView. snp_makeConstraints { ( make) -> Void in
187+ make. edges. equalTo ( scrollView)
188+ make. height. equalTo ( topScrollHeight)
189+ }
190+
191+ for i in 0 ... 40 {
192+ let box = UIView ( )
193+ box. backgroundColor = UIColor . redColor ( )
194+ containerView. addSubview ( box)
195+
196+ box. snp_makeConstraints ( closure: { ( make) -> Void in
197+ make. top. height. equalTo ( containerView) // 确定top和height之后,box在竖直方向上完全确定
198+ make. width. equalTo ( boxWidth)
199+ if i == 0 {
200+ make. left. equalTo ( containerView) . offset ( boxGap / 2 )
201+ }
202+ else if let previousBox = containerView. subviews [ i - 1 ] as? UIView {
203+ make. left. equalTo ( previousBox. snp_right) . offset ( boxGap)
204+ }
205+ if i == 40 {
206+ containerView. snp_makeConstraints ( closure: { ( make) -> Void in
207+ // 这一步是关键,它确定了container的宽度,也就确定了contentSize
208+ // This step is very important, it set the width of container, so the
209+ // contentSize is available now
210+ make. right. equalTo ( box) . offset ( boxGap / 2 )
211+ } )
212+ }
213+ } )
214+ }
215+ }
216+ }
153217
154218
155219
0 commit comments