-
Notifications
You must be signed in to change notification settings - Fork 899
/
AllTest12ViewController.m
218 lines (155 loc) · 8.94 KB
/
AllTest12ViewController.m
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// AllTest11ViewController.m
// MyLayoutDemo
//
// Created by oubaiquan on 2018/8/1.
// Copyright © 2018年 YoungSoft. All rights reserved.
//
#import "AllTest12ViewController.h"
#import "MyLayout.h"
#import "CFTool.h"
@interface AllTest12ViewController ()
@end
@implementation AllTest12ViewController
- (void)viewDidLoad {
self.edgesForExtendedLayout = UIRectEdgeNone; //设置视图控制器中的视图尺寸不延伸到导航条或者工具条下面。您可以注释这句代码看看效果。
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// Do any additional setup after loading the view.
/*
这个DEMO演示MyLayout和AutoLayout的代码结合的例子。因为布局视图也是一个普通的视图,因此可以把一个布局视图添加到现有的其他非布局父视图中并且对布局视图设置
约束。 因为布局视图可以设置尺寸自适应,就如UILabel一样因为具有intrinsicContentSize的能力,因此不需要在约束中明确设置宽度或者高度约束。当一个布局视图
的高度或者宽度都是由子视图决定的,也就是尺寸设置为自适应时,布局视图也不需要明确的设置宽度或者高度的约束。而且其他视图还可以依赖这种布局视图尺寸的自包含的能力。
本例子由3个子示例组成。
*/
//容器视图包含一个垂直线性布局视图,垂直线性布局视图的尺寸由2个子视图决定,容器视图的尺寸由线性布局视图决定
[self demo1];
//一个线性布局视图的宽度为某个具体的约束值,高度由子视图决定。另外一个兄弟视图在线性布局视图的下面,一个兄弟视图在线性布局的右边。
[self demo2];
//一个水平线性布局的高度固定,宽度自适应,另外一个兄弟视图在水平线性布局视图的右边。
[self demo3];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
-(void)demo1
{
//容器视图包含一个垂直线性布局视图,垂直线性布局视图的尺寸由2个子视图决定,容器视图的尺寸由线性布局视图决定
UIView *containerView = [UIView new];
containerView.backgroundColor = [UIColor greenColor];
containerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:containerView];
MyLinearLayout *linelayout = [MyLinearLayout new];
linelayout.translatesAutoresizingMaskIntoConstraints = NO;
linelayout.backgroundColor = [UIColor redColor];
[containerView addSubview:linelayout];
UIView *sbv1 = [UIView new];
sbv1.backgroundColor = [UIColor blueColor];
[linelayout addSubview:sbv1];
UIView *sbv2 = [UIView new];
sbv2.backgroundColor = [UIColor blueColor];
[linelayout addSubview:sbv2];
//MyLayout中的约束设置方法
linelayout.orientation = MyOrientation_Vert;
linelayout.mySize = CGSizeMake(MyLayoutSize.wrap, MyLayoutSize.wrap);
linelayout.padding = UIEdgeInsetsMake(10, 10, 10, 10);
linelayout.subviewSpace = 10;
sbv1.mySize = CGSizeMake(100, 40);
sbv2.mySize = CGSizeMake(150, 50);
//AutoLayout中的约束设置方法,采用iOS9提供的约束设置方法。
[containerView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
[containerView.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES;
//父视图约束依赖子视图约束会产生父视图的尺寸由子视图进行自适应的效果。
[containerView.bottomAnchor constraintEqualToAnchor:linelayout.bottomAnchor constant:10].active = YES;
[containerView.rightAnchor constraintEqualToAnchor:linelayout.rightAnchor constant:10].active = YES;
//线性布局视图的宽度和高度自适应,所以不需要设置任何高度和宽度的约束。
[linelayout.leftAnchor constraintEqualToAnchor:containerView.leftAnchor constant:10].active = YES;
[linelayout.topAnchor constraintEqualToAnchor:containerView.topAnchor constant:10].active = YES;
}
-(void)demo2
{
//一个线性布局视图的宽度为某个具体的约束值,高度由子视图决定。另外一个兄弟视图在线性布局视图的下面,一个兄弟视图在线性布局的右边。
MyLinearLayout *linelayout = [MyLinearLayout new];
linelayout.translatesAutoresizingMaskIntoConstraints = NO;
linelayout.backgroundColor = [UIColor redColor];
[self.view addSubview:linelayout];
UIView *sbv1 = [UIView new];
sbv1.backgroundColor = [UIColor blueColor];
[linelayout addSubview:sbv1];
UIView *sbv2 = [UIView new];
sbv2.backgroundColor = [UIColor blueColor];
[linelayout addSubview:sbv2];
UIView *brotherView1 = [UIView new];
brotherView1.translatesAutoresizingMaskIntoConstraints = NO;
brotherView1.backgroundColor = [UIColor greenColor];
[self.view addSubview:brotherView1];
UIView *brotherView2 = [UIView new];
brotherView2.translatesAutoresizingMaskIntoConstraints = NO;
brotherView2.backgroundColor = [UIColor orangeColor];
[self.view addSubview:brotherView2];
//MyLayout中的约束设置
linelayout.orientation = MyOrientation_Vert;
linelayout.myHeight = MyLayoutSize.wrap;
linelayout.padding = UIEdgeInsetsMake(10, 10, 10, 10);
linelayout.subviewSpace = 10;
sbv1.mySize = CGSizeMake(100, 40);
sbv2.mySize = CGSizeMake(150, 50);
//AutoLayout中的约束设置
//线性布局因为高度是自适应的所以不需要设置高度约束。
[linelayout.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:10].active = YES;
[linelayout.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:180].active = YES;
//当布局视图的高度由子视图自适应且宽度为autolayout约束时,目前只支持对布局视图设置明确宽度约束才有效。
//同时设置左右边界约束来推导布局视图宽度,并且高度又自适应目前不支持。
[linelayout.widthAnchor constraintEqualToAnchor:self.view.widthAnchor constant:-150].active = YES;
[brotherView1.leftAnchor constraintEqualToAnchor:linelayout.leftAnchor].active = YES;
[brotherView1.topAnchor constraintEqualToAnchor:linelayout.bottomAnchor constant:10].active = YES;
[brotherView1.widthAnchor constraintEqualToAnchor:linelayout.widthAnchor].active = YES;
[brotherView1.heightAnchor constraintEqualToAnchor:linelayout.heightAnchor].active = YES;
[brotherView2.leftAnchor constraintEqualToAnchor:linelayout.rightAnchor constant:10].active = YES;
[brotherView2.topAnchor constraintEqualToAnchor:linelayout.topAnchor].active = YES;
[brotherView2.widthAnchor constraintEqualToConstant:50].active = YES;
[brotherView2.heightAnchor constraintEqualToAnchor:linelayout.heightAnchor].active = YES;
}
-(void)demo3
{
MyLinearLayout *linelayout = [MyLinearLayout new];
linelayout.translatesAutoresizingMaskIntoConstraints = NO;
linelayout.backgroundColor = [UIColor redColor];
[self.view addSubview:linelayout];
UIView *sbv1 = [UIView new];
sbv1.backgroundColor = [UIColor blueColor];
[linelayout addSubview:sbv1];
UIView *sbv2 = [UIView new];
sbv2.backgroundColor = [UIColor blueColor];
[linelayout addSubview:sbv2];
UIView *brotherView1 = [UIView new];
brotherView1.translatesAutoresizingMaskIntoConstraints = NO;
brotherView1.backgroundColor = [UIColor greenColor];
[self.view addSubview:brotherView1];
//MyLayout中的约束设置
linelayout.orientation = MyOrientation_Horz;
linelayout.myWidth = MyLayoutSize.wrap;
linelayout.padding = UIEdgeInsetsMake(10, 10, 10, 10);
linelayout.subviewSpace = 10;
linelayout.gravity = MyGravity_Vert_Fill;
sbv1.myWidth = 50;
sbv2.myWidth = 60;
//AutoLayout中的约束设置
[linelayout.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:10].active = YES;
[linelayout.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:500].active = YES;
[linelayout.heightAnchor constraintEqualToConstant:60].active = YES;
[brotherView1.leftAnchor constraintEqualToAnchor:linelayout.rightAnchor constant:10].active = YES;
[brotherView1.topAnchor constraintEqualToAnchor:linelayout.topAnchor].active = YES;
[brotherView1.widthAnchor constraintEqualToAnchor:linelayout.widthAnchor].active = YES;
[brotherView1.heightAnchor constraintEqualToAnchor:linelayout.heightAnchor].active = YES;
}
@end