标签:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import UIKit import SnapKit class ViewController : UIViewController { lazy var box = UIView () override func viewDidLoad() { super .viewDidLoad() box.backgroundColor = UIColor .orangeColor() self .view.addSubview(box) box.snp_makeConstraints { (make) -> Void in make.width.equalTo(100) make.height.equalTo(100) make.center.equalTo( self .view) } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import UIKit import SnapKit class ViewController : UIViewController { lazy var box = UIView () override func viewDidLoad() { super .viewDidLoad() box.backgroundColor = UIColor .orangeColor() self .view.addSubview(box) box.snp_makeConstraints { (make) -> Void in make.width.height.equalTo(100) make.center.equalTo( self .view) } } } |
视图属性(ViewAttribute) | 布局属性(NSLayoutAttribute) |
view.snp_left | NSLayoutAttribute.Left |
view.snp_right | NSLayoutAttribute.Right |
view.snp_top | NSLayoutAttribute.Top |
view.snp_bottom | NSLayoutAttribute.Bottom |
view.snp_leading | NSLayoutAttribute.Leading |
view.snp_trailing | NSLayoutAttribute.Trailing |
view.snp_width | NSLayoutAttribute.Width |
view.snp_height | NSLayoutAttribute.Height |
view.snp_centerX | NSLayoutAttribute.CenterX |
view.snp_centerY | NSLayoutAttribute.CenterY |
view.snp_baseline | NSLayoutAttribute.Baseline |
1
2
|
//使当前视图对象的中心x坐标小于等于视图view2的左边的x坐标 make.centerX.lessThanOrEqualTo(view2.snp_left) |
1
|
make.left.greaterThanOrEqualTo(label) |
1
|
make.left.greaterThanOrEqualTo(label.snp_left) |
1
2
3
|
make.height.equalTo(20) make.width.equalTo(20) make.top.equalTo(42) |
1
2
3
4
|
make.height.equalTo(20) make.width.equalTo( self .buttonSize.width) //当前视图与label的顶部齐平 make.top.equalTo(label.snp_top) |
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
28
29
30
|
import UIKit import SnapKit class ViewController : UIViewController { //外部方块 lazy var boxOutter = UIView () //内部方块 lazy var boxInner = UIView () override func viewDidLoad() { super .viewDidLoad() boxOutter.backgroundColor = UIColor .orangeColor() self .view.addSubview(boxOutter) boxInner.backgroundColor = UIColor .greenColor() boxOutter.addSubview(boxInner) boxOutter.snp_makeConstraints { (make) -> Void in make.width.height.equalTo(200) make.center.equalTo( self .view) } boxInner.snp_makeConstraints { (make) -> Void in make.width.height.equalTo(100) make.right.equalTo(0) make.bottom.equalTo(0) } } } |
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
28
29
30
31
32
|
import UIKit import SnapKit class ViewController : UIViewController { //方块1 lazy var box1 = UIView () //方块2 lazy var box2 = UIView () override func viewDidLoad() { super .viewDidLoad() box1.backgroundColor = UIColor .orangeColor() self .view.addSubview(box1) box2.backgroundColor = UIColor .greenColor() self .view.addSubview(box2) box1.snp_makeConstraints { (make) -> Void in make.left.equalTo(20) make.right.equalTo(-20) make.height.equalTo(40) make.top.equalTo(20) } box2.snp_makeConstraints { (make) -> Void in make.width.height.equalTo(box1) make.left.equalTo(box1) //等同于 make.left.equalTo(box1.snp_left) make.top.equalTo(box1.snp_bottom) } } } |
1
2
|
//让当前视图 的 上下左右(top,left,bottom,right) 等于 view2 make.edges.equalTo(view2) |
1
2
|
//当前视图宽高 >= titleLabel make.size.greaterThanOrEqualTo(titleLabel) |
1
2
|
//当前视图与 button1中心相同 (centerX 和 centerY) make.center.equalTo(button1) |
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
28
|
import UIKit import SnapKit class ViewController : UIViewController { //外部方块 lazy var boxOutter = UIView () //内部方块 lazy var boxInner = UIView () override func viewDidLoad() { super .viewDidLoad() boxOutter.backgroundColor = UIColor .orangeColor() self .view.addSubview(boxOutter) boxInner.backgroundColor = UIColor .greenColor() boxOutter.addSubview(boxInner) boxOutter.snp_makeConstraints { (make) -> Void in make.width.height.equalTo(200) make.center.equalTo( self .view) } boxInner.snp_makeConstraints { (make) -> Void in make.edges.equalTo(boxOutter).inset( UIEdgeInsetsMake (10, 15, 20, 25)) } } } |
1
2
3
4
|
make.top.equalTo(boxOutter).offset(10) make.left.equalTo(boxOutter).offset(15) make.bottom.equalTo(boxOutter).offset(-20) make.right.equalTo(boxOutter).offset(-25) |
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
28
29
30
|
import UIKit import SnapKit class ViewController : UIViewController { //外部方块 lazy var boxOutter = UIView () //内部方块 lazy var boxInner = UIView () override func viewDidLoad() { super .viewDidLoad() boxOutter.backgroundColor = UIColor .orangeColor() self .view.addSubview(boxOutter) boxInner.backgroundColor = UIColor .greenColor() boxOutter.addSubview(boxInner) boxOutter.snp_makeConstraints { (make) -> Void in make.width.height.equalTo(200) make.center.equalTo( self .view) } boxInner.snp_makeConstraints { (make) -> Void in make.center.equalTo(boxOutter) // make width = superview.width + 100, height = superview.height - 50 make.size.equalTo(boxOutter).offset( CGSizeMake (50, -50)) } } } |
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
28
29
30
|
import UIKit import SnapKit class ViewController : UIViewController { //外部方块 lazy var boxOutter = UIView () //内部方块 lazy var boxInner = UIView () override func viewDidLoad() { super .viewDidLoad() boxOutter.backgroundColor = UIColor .orangeColor() self .view.addSubview(boxOutter) boxInner.backgroundColor = UIColor .greenColor() boxOutter.addSubview(boxInner) boxOutter.snp_makeConstraints { (make) -> Void in make.width.height.equalTo(200) make.center.equalTo( self .view) } boxInner.snp_makeConstraints { (make) -> Void in make.center.equalTo(boxOutter) // make width = superview.width / 2, height = superview.height / 2 make.size.equalTo(boxOutter).multipliedBy(0.5) } } } |
二、Swift - 自动布局库SnapKit的使用详解2(约束的更新、移除、重做)
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
28
29
|
import UIKit import SnapKit class ViewController : UIViewController { lazy var box = UIView () //保存约束的引用 var topConstraint: Constraint ? override func viewDidLoad() { super .viewDidLoad() box.backgroundColor = UIColor .orangeColor() self .view.addSubview(box) box.snp_makeConstraints { (make) -> Void in make.width.height.equalTo(150) make.centerX.equalTo( self .view) self .topConstraint = make.top.equalTo( self .view).offset(40).constraint } } //按钮点击 @IBAction func btnTouch(sender: AnyObject ) { //移除约束 self .topConstraint?.uninstall() } } |
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
28
29
|
import UIKit import SnapKit class ViewController : UIViewController { lazy var box = UIView () //保存约束的引用 var topConstraint: Constraint ? override func viewDidLoad() { super .viewDidLoad() box.backgroundColor = UIColor .orangeColor() self .view.addSubview(box) box.snp_makeConstraints { (make) -> Void in make.width.height.equalTo(150) make.centerX.equalTo( self .view) self .topConstraint = make.top.equalTo( self .view).offset(40).constraint } } //按钮点击 @IBAction func btnTouch(sender: AnyObject ) { //更新修改约束 self .topConstraint?.updateOffset(60) } } |
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
28
29
30
|
import UIKit import SnapKit class ViewController : UIViewController { lazy var box = UIView () override func viewDidLoad() { super .viewDidLoad() box.backgroundColor = UIColor .orangeColor() self .view.addSubview(box) box.snp_makeConstraints { (make) -> Void in make.height.equalTo(150) make.centerX.equalTo( self .view) } } //视图约束更新 override func updateViewConstraints() { self .box.snp_updateConstraints{ (make) -> Void in //视图宽度与屏幕等宽 make.width.equalTo( self .view) } super .updateViewConstraints() } } |
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
|
import UIKit import SnapKit class ViewController : UIViewController { lazy var box = UIView () override func viewDidLoad() { super .viewDidLoad() box.backgroundColor = UIColor .orangeColor() self .view.addSubview(box) box.snp_makeConstraints { (make) -> Void in make.width.height.equalTo(150) make.centerX.equalTo( self .view) } } //按钮点击 @IBAction func btnTouch(sender: AnyObject ) { //重做约束 box.snp_remakeConstraints { (make) -> Void in make.width.height.equalTo(100) } } } |
三、Swift - 自动布局库SnapKit的使用详解3(约束优先级,约束做动画)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
import UIKit import SnapKit class ViewController : UIViewController { lazy var box = UIView () var scacle = 1.0 override func viewDidLoad() { super .viewDidLoad() //单击监听 let tapSingle= UITapGestureRecognizer (target: self ,action:#selector(tapSingleDid)) tapSingle.numberOfTapsRequired=1 tapSingle.numberOfTouchesRequired=1 self .view.addGestureRecognizer(tapSingle) box.backgroundColor = UIColor .orangeColor() self .view.addSubview(box) box.snp_makeConstraints { (make) -> Void in //视图居中 make.center.equalTo( self .view) //初始宽、高为100(优先级低) make.width.height.equalTo(100 * self .scacle).priorityLow(); //最大尺寸不能超过屏幕 make.width.height.lessThanOrEqualTo( self .view.snp_width) make.width.height.lessThanOrEqualTo( self .view.snp_height) } } //点击屏幕 func tapSingleDid(){ self .scacle += 0.5 self .box.snp_updateConstraints{ (make) -> Void in //放大视图(优先级最低) make.width.height.equalTo(100 * self .scacle).priorityLow(); } } } |
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
import UIKit import SnapKit class ViewController : UIViewController { lazy var box = UIView () var scacle = 1.0 override func viewDidLoad() { super .viewDidLoad() //单击监听 let tapSingle= UITapGestureRecognizer (target: self ,action:#selector(tapSingleDid)) tapSingle.numberOfTapsRequired=1 tapSingle.numberOfTouchesRequired=1 self .view.addGestureRecognizer(tapSingle) box.backgroundColor = UIColor .orangeColor() self .view.addSubview(box) box.snp_makeConstraints { (make) -> Void in //视图居中 make.center.equalTo( self .view) //初始宽、高为100(优先级低) make.width.height.equalTo(100 * self .scacle).priorityLow(); //最大尺寸不能超过屏幕 make.width.height.lessThanOrEqualTo( self .view.snp_width) make.width.height.lessThanOrEqualTo( self .view.snp_height) } } //视图约束更新 override func updateViewConstraints() { self .box.snp_updateConstraints{ (make) -> Void in //放大尺寸(优先级低) make.width.height.equalTo(100 * self .scacle).priorityLow(); } super .updateViewConstraints() } //点击屏幕 func tapSingleDid(){ self .scacle += 0.5 //告诉self.view约束需要更新 self .view.setNeedsUpdateConstraints() //动画 UIView .animateWithDuration(0.3) { self .view.layoutIfNeeded() } } } |
四、Swift - 自动布局库SnapKit的使用详解4(样例1:实现一个登录页面)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
import UIKit import SnapKit class ViewController : UIViewController , UITextFieldDelegate { var txtUser: UITextField ! //用户名输入框 var txtPwd: UITextField ! //密码输入款 var formView: UIView ! //登陆框视图 var horizontalLine: UIView ! //分隔线 var confirmButton: UIButton ! //登录按钮 var titleLabel: UILabel ! //标题标签 var topConstraint: Constraint ? //登录框距顶部距离约束 override func viewDidLoad() { super .viewDidLoad() //视图背景色 self .view.backgroundColor = UIColor (red: 1/255, green: 170/255, blue: 235/255, alpha: 1) //登录框高度 let formViewHeight = 90 //登录框背景 self .formView = UIView () self .formView.layer.borderWidth = 0.5 self .formView.layer.borderColor = UIColor .lightGrayColor(). CGColor self .formView.backgroundColor = UIColor .whiteColor() self .formView.layer.cornerRadius = 5 self .view.addSubview( self .formView) //最常规的设置模式 self .formView.snp_makeConstraints { (make) -> Void in make.left.equalTo(15) make.right.equalTo(-15) //存储top属性 self .topConstraint = make.centerY.equalTo( self .view).constraint make.height.equalTo(formViewHeight) } //分隔线 self .horizontalLine = UIView () self .horizontalLine.backgroundColor = UIColor .lightGrayColor() self .formView.addSubview( self .horizontalLine) self .horizontalLine.snp_makeConstraints { (make) -> Void in make.height.equalTo(0.5) make.left.equalTo(15) make.right.equalTo(-15) make.centerY.equalTo( self .formView) } //密码图 let imgLock1 = UIImageView (frame: CGRectMake (11, 11, 22, 22)) imgLock1.image = UIImage (named: "iconfont-user" ) //密码图 let imgLock2 = UIImageView (frame: CGRectMake (11, 11, 22, 22)) imgLock2.image = UIImage (named: "iconfont-password" ) //用户名输入框 self .txtUser = UITextField () self .txtUser.delegate = self self .txtUser.placeholder = "用户名" self .txtUser.tag = 100 self .txtUser.leftView = UIView (frame: CGRectMake (0, 0, 44, 44)) self .txtUser.leftViewMode = UITextFieldViewMode . Always self .txtUser.returnKeyType = UIReturnKeyType . Next //用户名输入框左侧图标 self .txtUser.leftView!.addSubview(imgLock1) self .formView.addSubview( self .txtUser) //布局 self .txtUser.snp_makeConstraints { (make) -> Void in make.left.equalTo(15) make.right.equalTo(-15) make.height.equalTo(44) make.centerY.equalTo(0).offset(-formViewHeight/4) } //密码输入框 self .txtPwd = UITextField () self .txtPwd.delegate = self self .txtPwd.placeholder = "密码" self .txtPwd.tag = 101 self .txtPwd.leftView = UIView (frame: CGRectMake (0, 0, 44, 44)) self .txtPwd.leftViewMode = UITextFieldViewMode . Always self .txtPwd.returnKeyType = UIReturnKeyType . Next //密码输入框左侧图标 self .txtPwd.leftView!.addSubview(imgLock2) self .formView.addSubview( self .txtPwd) //布局 self .txtPwd.snp_makeConstraints { (make) -> Void in make.left.equalTo(15) make.right.equalTo(-15) make.height.equalTo(44) make.centerY.equalTo(0).offset(formViewHeight/4) } //登录按钮 self .confirmButton = UIButton () self .confirmButton.setTitle( "登录" , forState: UIControlState . Normal ) self .confirmButton.setTitleColor( UIColor .blackColor(), forState: UIControlState . Normal ) self .confirmButton.layer.cornerRadius = 5 self .confirmButton.backgroundColor = UIColor (colorLiteralRed: 1, green: 1, blue: 1, alpha: 0.5) self .confirmButton.addTarget( self , action: #selector(loginConfrim), forControlEvents: . TouchUpInside ) self .view.addSubview( self .confirmButton) self .confirmButton.snp_makeConstraints { (make) -> Void in make.left.equalTo(15) make.top.equalTo( self .formView.snp_bottom).offset(20) make.right.equalTo(-15) make.height.equalTo(44) } //标题label self .titleLabel = UILabel () self .titleLabel.text = "hangge.com" self .titleLabel.textColor = UIColor .whiteColor() self .titleLabel.font = UIFont .systemFontOfSize(36) self .view.addSubview( self .titleLabel) self .titleLabel.snp_makeConstraints { (make) -> Void in make.bottom.equalTo( self .formView.snp_top).offset(-20) make.centerX.equalTo(0) make.height.equalTo(44) } } //输入框获取焦点开始编辑 func textFieldDidBeginEditing(textField: UITextField ) { UIView .animateWithDuration(0.5, animations: { () -> Void in self .topConstraint?.updateOffset(-125) self .view.layoutIfNeeded() }) } //输入框返回时操作 func textFieldShouldReturn(textField: UITextField ) -> Bool { let tag = textField.tag switch tag { case 100: self .txtPwd.becomeFirstResponder() case 101: loginConfrim() default : print (textField.text) } return true } //登录按钮点击 func loginConfrim(){ //收起键盘 self .view.endEditing( true ) //视图约束恢复初始设置 UIView .animateWithDuration(0.5, animations: { () -> Void in self .topConstraint?.updateOffset(0) self .view.layoutIfNeeded() }) } } |
五、Swift - 自动布局库SnapKit的使用详解5(样例2:实现一个计算器界面)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
import UIKit import SnapKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() //申明区域,keyboardView是键盘区域 let keyboardView = UIView () keyboardView.backgroundColor = UIColor (red:212/255, green:213/255, blue:216/255, alpha:1) self .view.addSubview(keyboardView) //键盘区域约束 keyboardView.snp_makeConstraints { (make) -> Void in make.bottom.equalTo( self .view.snp_bottom) make.left.right.equalTo( self .view) make.height.equalTo( self .view.snp_width).multipliedBy(1.25) } //displayView是显示区 let displayView = UIView () displayView.backgroundColor = UIColor .blackColor() self .view.addSubview(displayView) //显示区域约束 displayView.snp_makeConstraints { (make) -> Void in make.top.equalTo( self .view.snp_top) make.bottom.equalTo(keyboardView.snp_top) make.left.right.equalTo( self .view) } //设置显示位置的数字为0 let displayNum = UILabel () displayNum.text = "0" displayNum.font = UIFont (name: "HeiTi SC" , size:70) displayNum.textColor = UIColor .whiteColor() displayNum.textAlignment = . Right displayView.addSubview(displayNum) //数字标签约束 displayNum.snp_makeConstraints { (make) -> Void in make.left.right.equalTo(displayView).offset(-10) make.bottom.equalTo(displayView).offset(-10) } //定义键盘键名称(?号是占位符,代表合并的单元格) let keys = [ "AC" , "+/-" , "%" , "÷" , "7" , "8" , "9" , "x" , "4" , "5" , "6" , "-" , "1" , "2" , "3" , "+" , "0" , "?" , "." , "=" ] //保存所有的按钮 var keyViews = [ UIButton ]() //循环添加按钮 for i in 0..<5{ for j in 0..<4 { let indexOfKeys = i * 4 + j let key = keys[indexOfKeys] //键样式 let keyView = UIButton (type:. Custom ) keyboardView.addSubview(keyView) keyViews.append(keyView) keyView.setTitleColor( UIColor .blackColor(), forState: . Normal ) keyView.setTitle(key, forState: . Normal ) keyView.layer.borderWidth = 0.5 keyView.layer.borderColor = UIColor .blackColor(). CGColor keyView.titleLabel?.font = UIFont (name: "Arial-BoldItalicMT" , size:30) //处理合并单元格(不用添加到界面上,也不用添加约束) if key == "?" { keyView.removeFromSuperview() continue } //设置按键约束 keyView.snp_makeConstraints { (make) -> Void in //添加高度约束 make.height.equalTo(keyboardView.snp_height).multipliedBy(0.2) //添加宽度约束 if key == "0" { //处理 0 make.width.equalTo(keyboardView.snp_width).multipliedBy(0.5) } else { //正常的单元格 make.width.equalTo(keyboardView.snp_width).multipliedBy(0.25) } //添加垂直位置约束 if i == 0{ make.top.equalTo(0) keyView.backgroundColor = UIColor (red:201/255, green:202/255, blue:204/255, alpha:1) } else { make.top.equalTo(keyViews[indexOfKeys-4].snp_bottom) } //添加水平位置约束 switch (j) { case 0: make.left.equalTo(keyboardView.snp_left) case 1: make.right.equalTo(keyboardView.snp_centerX) case 2: make.left.equalTo(keyboardView.snp_centerX) case 3: make.right.equalTo(keyboardView.snp_right) keyView.backgroundColor = UIColor (red:249/255, green:138/255, blue:17/255, alpha:1) default : break } } } } } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() } } |
链接:
标签:
原文地址:http://www.cnblogs.com/On1Key/p/5756343.html