Skip to content

Commit e3c5bc1

Browse files
committed
Add some comments
1 parent cb64555 commit e3c5bc1

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

AutolayoutScrollViewInCode/ViewController.swift

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ let boxGap: CGFloat = 50
1717

1818
class ViewController: UIViewController {
1919

20-
var scrollView = UIScrollView()
21-
var containerView = UIView()
20+
let scrollView = UIScrollView()
21+
let containerView = UIView()
2222

2323

2424
override func viewDidLoad() {
@@ -33,8 +33,8 @@ class ViewController: UIViewController {
3333
使用外部视图进行布局
3434
Use views outside to locate subviews in scrollview
3535
*/
36-
// layoutWithContainer()
37-
layoutWithAbsoluteView()
36+
layoutWithContainer()
37+
// layoutWithAbsoluteView()
3838
}
3939

4040
override func didReceiveMemoryWarning() {
@@ -59,12 +59,21 @@ extension ViewController {
5959

6060
containerView.backgroundColor = scrollView.backgroundColor
6161

62+
/**
63+
* 对scrollView添加约束
64+
* Add constraints to scrollView
65+
*/
6266
scrollView.snp_makeConstraints { (make) -> Void in
6367
make.centerY.equalTo(view.snp_centerY)
6468
make.left.right.equalTo(view)
6569
make.height.equalTo(topScrollHeight)
6670
}
6771

72+
/**
73+
* 对containerView添加约束,接下来只要确定containerView的宽度即可
74+
* Add constraints to containerView, the only thing we will do
75+
* is to define the width of containerView
76+
*/
6877
containerView.snp_makeConstraints { (make) -> Void in
6978
make.edges.equalTo(scrollView)
7079
make.height.equalTo(topScrollHeight)
@@ -76,7 +85,7 @@ extension ViewController {
7685
containerView.addSubview(box)
7786

7887
box.snp_makeConstraints(closure: { (make) -> Void in
79-
make.top.height.equalTo(containerView)
88+
make.top.height.equalTo(containerView) // 确定top和height之后,box在竖直方向上完全确定
8089
make.width.equalTo(boxWidth)
8190
if i == 0 {
8291
make.left.equalTo(containerView).offset(boxGap / 2)
@@ -86,6 +95,9 @@ extension ViewController {
8695
}
8796
if i == 5 {
8897
containerView.snp_makeConstraints(closure: { (make) -> Void in
98+
// 这一步是关键,它确定了container的宽度,也就确定了contentSize
99+
// This step is very important, it set the width of container, so the
100+
// contentSize is available now
89101
make.right.equalTo(box)
90102
})
91103
}
@@ -111,9 +123,12 @@ extension ViewController {
111123
box.backgroundColor = UIColor.redColor()
112124
scrollView.addSubview(box)
113125

126+
// box依赖于外部视图布局,不能依赖scrollView
127+
// The position of box rely on self.view instead of scrollView
114128
box.snp_makeConstraints(closure: { (make) -> Void in
115129
make.top.equalTo(0)
116-
make.bottom.equalTo(view).offset(-(ScreenHeight - topScrollHeight) / 2) // This bottom can be incorret when device is rotated
130+
// This bottom can be incorret when device is rotated
131+
make.bottom.equalTo(view).offset(-(ScreenHeight - topScrollHeight) / 2)
117132
make.height.equalTo(topScrollHeight)
118133

119134
make.width.equalTo(boxWidth)
@@ -125,6 +140,9 @@ extension ViewController {
125140
}
126141

127142
if i == 5 {
143+
// 这里设定最右侧的box,距离contentSize的右边界距离
144+
// The the distance from the box on the right side
145+
// to the right side of contentSize
128146
make.right.equalTo(scrollView)
129147
}
130148
})

0 commit comments

Comments
 (0)