Skip to content
Navigation Menu
{{ message }}
forked from youngsoft/MyLinearLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLTest3ViewController.m
More file actions
416 lines (306 loc) · 15.3 KB
/
Copy pathLLTest3ViewController.m
File metadata and controls
416 lines (306 loc) · 15.3 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//
// Test3ViewController.m
// MyLayout
//
// Created by oybq on 15/6/21.
// Copyright (c) 2015年 YoungSoft. All rights reserved.
//
#import "LLTest3ViewController.h"
#import "MyLayout.h"
@interface LLTest3ViewController ()
@property(nonatomic, strong) MyLinearLayout *vertGravityLayout;
@property(nonatomic, strong) MyLinearLayout *horzGravityLayout;
@end
@implementation LLTest3ViewController
-(void)addVertGravityLayoutItems:(MyLinearLayout*)layout
{
UILabel *v1 = [UILabel new];
v1.text = @"测试文本1";
[v1 sizeToFit];
v1.backgroundColor = [UIColor redColor];
[layout addSubview:v1];
UILabel *v2 = [UILabel new];
v2.text = @"测试文本2测试文本2";
[v2 sizeToFit];
v2.backgroundColor = [UIColor greenColor];
[layout addSubview:v2];
UILabel *v3 = [UILabel new];
v3.text = @"测试文本3测试文本3测试文本3";
[v3 sizeToFit];
v3.backgroundColor = [UIColor blueColor];
[layout addSubview:v3];
UILabel *v4 = [UILabel new];
v4.text = @"你可以自定义左右边距和宽度";
[v4 sizeToFit];
v4.backgroundColor = [UIColor orangeColor];
v4.myLeftMargin = 20;
v4.myRightMargin = 30;
[layout addSubview:v4];
}
-(void)addHorzGravityLayoutItems:(MyLinearLayout*)layout
{
UILabel *v1 = [UILabel new];
v1.text = @"测试1";
v1.numberOfLines = 0;
[v1 sizeToFit];
v1.backgroundColor = [UIColor redColor];
[layout addSubview:v1];
UILabel *v2 = [UILabel new];
v2.text = @"测试2\n测试2";
v2.numberOfLines = 0;
[v2 sizeToFit];
v2.backgroundColor = [UIColor greenColor];
[layout addSubview:v2];
UILabel *v3 = [UILabel new];
v3.text = @"测试3\n测试3\n测试3";
v3.numberOfLines = 0;
[v3 sizeToFit];
v3.backgroundColor = [UIColor blueColor];
[layout addSubview:v3];
UILabel *v4 = [UILabel new];
v4.text = @"你可以\n自定义\n左右边\n距和宽度";
v4.numberOfLines = 0;
v4.adjustsFontSizeToFitWidth = YES;
[v4 sizeToFit];
v4.backgroundColor = [UIColor orangeColor];
v4.myTopMargin = 20;
v4.myBottomMargin = 10;
[layout addSubview:v4];
}
-(MyFlowLayout*)createVertGravityLayoutActionLayout
{
//流式布局后面的例子有说明
MyFlowLayout *flowLayout1 = [MyFlowLayout flowLayoutWithOrientation:MyLayoutViewOrientation_Vert arrangedCount:3];
flowLayout1.wrapContentHeight = YES;
flowLayout1.averageArrange = YES;
flowLayout1.subviewHorzMargin = 5;
flowLayout1.subviewVertMargin = 5;
flowLayout1.padding = UIEdgeInsetsMake(5, 5, 5, 5);
UIButton *topButton = [self createActionButton:@"上停靠" tag:100];
[flowLayout1 addSubview:topButton];
UIButton *centerVertButton = [self createActionButton:@"垂直居中停靠" tag:200];
[flowLayout1 addSubview:centerVertButton];
UIButton *bottomButton = [self createActionButton:@"下停靠" tag:300];
[flowLayout1 addSubview:bottomButton];
UIButton *leftButton = [self createActionButton:@"左停靠" tag:400];
[flowLayout1 addSubview:leftButton];
UIButton *centerHorzButton = [self createActionButton:@"水平居中停靠" tag:500];
[flowLayout1 addSubview:centerHorzButton];
UIButton *rightButton = [self createActionButton:@"右停靠" tag:600];
[flowLayout1 addSubview:rightButton];
UIButton *fillHorzButton = [self createActionButton:@"水平填充" tag:700];
[flowLayout1 addSubview:fillHorzButton];
UIButton *windowCenterVertButton = [self createActionButton:@"窗口垂直居中停靠" tag:800];
[flowLayout1 addSubview:windowCenterVertButton];
UIButton *windowCenterHorzButton = [self createActionButton:@"窗口水平居中停靠" tag:900];
[flowLayout1 addSubview:windowCenterHorzButton];
UIButton *subviewMarginButton = [self createActionButton:@"子视图间距设置" tag:1000];
[flowLayout1 addSubview:subviewMarginButton];
return flowLayout1;
}
-(MyFlowLayout*)createHorzGravityLayoutActionLayout
{
//流式布局后面的例子有说明
MyFlowLayout *flowLayout1 = [MyFlowLayout flowLayoutWithOrientation:MyLayoutViewOrientation_Vert arrangedCount:3];
flowLayout1.wrapContentHeight = YES;
flowLayout1.averageArrange = YES;
flowLayout1.subviewHorzMargin = 5;
flowLayout1.subviewVertMargin = 5;
flowLayout1.padding = UIEdgeInsetsMake(5, 5, 5, 5);
UIButton *leftButton = [self createActionButton:@"左停靠" tag:101];
[flowLayout1 addSubview:leftButton];
UIButton *centerHorzButton = [self createActionButton:@"水平居中停靠" tag:201];
[flowLayout1 addSubview:centerHorzButton];
UIButton *rightButton = [self createActionButton:@"右停靠" tag:301];
[flowLayout1 addSubview:rightButton];
UIButton *topButton = [self createActionButton:@"上停靠" tag:401];
[flowLayout1 addSubview:topButton];
UIButton *centerVertButton = [self createActionButton:@"垂直居中停靠" tag:501];
[flowLayout1 addSubview:centerVertButton];
UIButton *bottomButton = [self createActionButton:@"下停靠" tag:601];
[flowLayout1 addSubview:bottomButton];
UIButton *fillVertButton = [self createActionButton:@"垂直填充" tag:701];
[flowLayout1 addSubview:fillVertButton];
UIButton *windowCenterVertButton = [self createActionButton:@"窗口垂直居中停靠" tag:801];
[flowLayout1 addSubview:windowCenterVertButton];
UIButton *windowCenterHorzButton = [self createActionButton:@"窗口水平居中停靠" tag:901];
[flowLayout1 addSubview:windowCenterHorzButton];
return flowLayout1;
}
-(UIButton*)createActionButton:(NSString*)title tag:(NSInteger)tag
{
UIButton *actionButton = [UIButton new];
[actionButton setTitle:title forState:UIControlStateNormal];
[actionButton sizeToFit];
[actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
actionButton.titleLabel.adjustsFontSizeToFitWidth = YES;
actionButton.tag = tag;
[actionButton addTarget:self action:@selector(handleGravity:) forControlEvents:UIControlEventTouchUpInside];
actionButton.layer.borderColor = [UIColor grayColor].CGColor;
actionButton.layer.borderWidth = 1.0;
return actionButton;
}
-(void)loadView
{
UIScrollView *scrollView = [UIScrollView new];
self.view = scrollView;
scrollView.backgroundColor = [UIColor whiteColor];
MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
rootLayout.myLeftMargin = rootLayout.myRightMargin = 0;
[scrollView addSubview:rootLayout];
rootLayout.gravity = MyMarginGravity_Horz_Fill; //设置这个属性后rootLayout的所有子视图都不需要指定宽度了,这个值的意义是所有子视图的宽度都填充满布局。也就是说设置了这个值后不需要为每个子视图设置myLeftMargin, myRightMargin来指定宽度了。
UILabel *label1 = [UILabel new];
label1.text = @"垂直布局里面的停靠控制,您可以点击下面的不同停靠方式的按钮查看效果:";
label1.flexedHeight = YES;
label1.adjustsFontSizeToFitWidth = YES;
[rootLayout addSubview:label1];
[rootLayout addSubview:[self createVertGravityLayoutActionLayout]];
self.vertGravityLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
self.vertGravityLayout.myHeight = 200;
self.vertGravityLayout.myTopMargin = 10;
self.vertGravityLayout.myLeftMargin = 20;
self.vertGravityLayout.subviewMargin = 10;
self.vertGravityLayout.backgroundColor = [UIColor grayColor];
[rootLayout addSubview:self.vertGravityLayout];
[self addVertGravityLayoutItems:self.vertGravityLayout];
UILabel *label2 = [UILabel new];
label2.text = @"水平布局里面的停靠控制,您可以点击下面的不同停靠方式的按钮查看效果:";
label2.flexedHeight = YES;
label2.adjustsFontSizeToFitWidth = YES;
[rootLayout addSubview:label2];
[rootLayout addSubview:[self createHorzGravityLayoutActionLayout]];
self.horzGravityLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Horz];
self.horzGravityLayout.wrapContentWidth = NO;
self.horzGravityLayout.myHeight = 100;
self.horzGravityLayout.myTopMargin = 10;
self.horzGravityLayout.myLeftMargin = 20;
self.horzGravityLayout.subviewMargin = 5;
self.horzGravityLayout.backgroundColor = [UIColor grayColor];
[rootLayout addSubview:self.horzGravityLayout];
[self addHorzGravityLayoutItems:self.horzGravityLayout];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// self.title = @"线性布局-子视图的位置停靠";
[self handleNavTitleCenter:nil];
// self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithTitle:@"居中标题" style:UIBarButtonItemStylePlain target:self action:@selector(handleNavTitleCenter:) ],[[UIBarButtonItem alloc] initWithTitle:@"原始" style:UIBarButtonItemStylePlain target:self action:@selector(handleNavTitleDefault:)]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -- Handle Method
-(void)handleGravity:(UIButton*)button
{
switch (button.tag) {
case 100: //上
self.vertGravityLayout.gravity = (self.vertGravityLayout.gravity & MyMarginGravity_Vert_Mask) | MyMarginGravity_Vert_Top;
break;
case 200: //垂直
self.vertGravityLayout.gravity = (self.vertGravityLayout.gravity & MyMarginGravity_Vert_Mask) | MyMarginGravity_Vert_Center;
break;
case 300: //下
self.vertGravityLayout.gravity = (self.vertGravityLayout.gravity & MyMarginGravity_Vert_Mask) | MyMarginGravity_Vert_Bottom;
break;
case 400: //左
self.vertGravityLayout.gravity = (self.vertGravityLayout.gravity & MyMarginGravity_Horz_Mask) | MyMarginGravity_Horz_Left;
break;
case 500: //水平
self.vertGravityLayout.gravity = (self.vertGravityLayout.gravity & MyMarginGravity_Horz_Mask) | MyMarginGravity_Horz_Center;
break;
case 600: //右
self.vertGravityLayout.gravity = (self.vertGravityLayout.gravity & MyMarginGravity_Horz_Mask) | MyMarginGravity_Horz_Right;
break;
case 700: //填充
self.vertGravityLayout.gravity = (self.vertGravityLayout.gravity & MyMarginGravity_Horz_Mask) | MyMarginGravity_Horz_Fill;
break;
case 800: //窗口垂直居中
self.vertGravityLayout.gravity = (self.vertGravityLayout.gravity & MyMarginGravity_Vert_Mask) | MyMarginGravity_Vert_Window_Center;
break;
case 900: //窗口水平居中
self.vertGravityLayout.gravity = (self.vertGravityLayout.gravity & MyMarginGravity_Horz_Mask) | MyMarginGravity_Horz_Window_Center;
break;
case 1000:
{
self.vertGravityLayout.subviewMargin = self.vertGravityLayout.subviewMargin == 10 ? -10 : 10;
self.vertGravityLayout.beginLayoutBlock=^{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
};
self.vertGravityLayout.endLayoutBlock=^{
[UIView commitAnimations];
};
}
break;
case 101: //左
self.horzGravityLayout.gravity = (self.horzGravityLayout.gravity & MyMarginGravity_Horz_Mask) | MyMarginGravity_Horz_Left;
break;
case 201: //水平
self.horzGravityLayout.gravity = (self.horzGravityLayout.gravity & MyMarginGravity_Horz_Mask) | MyMarginGravity_Horz_Center;
break;
case 301: //右
self.horzGravityLayout.gravity = (self.horzGravityLayout.gravity & MyMarginGravity_Horz_Mask) | MyMarginGravity_Horz_Right;
break;
case 401: //上
self.horzGravityLayout.gravity = (self.horzGravityLayout.gravity & MyMarginGravity_Vert_Mask) | MyMarginGravity_Vert_Top;
break;
case 501: //垂直
self.horzGravityLayout.gravity = (self.horzGravityLayout.gravity & MyMarginGravity_Vert_Mask) | MyMarginGravity_Vert_Center;
break;
case 601: //下
self.horzGravityLayout.gravity = (self.horzGravityLayout.gravity & MyMarginGravity_Vert_Mask) | MyMarginGravity_Vert_Bottom;
break;
case 701: //填充
self.horzGravityLayout.gravity = (self.horzGravityLayout.gravity & MyMarginGravity_Vert_Mask) | MyMarginGravity_Vert_Fill;
break;
case 801: //窗口垂直居中
self.horzGravityLayout.gravity = (self.horzGravityLayout.gravity & MyMarginGravity_Vert_Mask) | MyMarginGravity_Vert_Window_Center;
break;
case 901: //窗口水平居中
self.horzGravityLayout.gravity = (self.horzGravityLayout.gravity & MyMarginGravity_Horz_Mask) | MyMarginGravity_Horz_Window_Center;
break;
default:
break;
}
}
#pragma mark -- Handle Method
-(void)handleNavTitleCenter:(id)sender
{
MyLinearLayout *navigationItemLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
navigationItemLayout.wrapContentHeight = navigationItemLayout.wrapContentWidth = NO;
//通过MyMarginGravity_Horz_Window_Center的设置总是保证在窗口的中间而不是布局视图的中间。
navigationItemLayout.gravity = MyMarginGravity_Horz_Window_Center | MyMarginGravity_Vert_Center;
navigationItemLayout.frame = self.navigationController.navigationBar.bounds;
UILabel *topLabel = [UILabel new];
topLabel.text = @"标题的在具有左边按钮和右边按钮时都可以居中";
topLabel.adjustsFontSizeToFitWidth = YES;
topLabel.textAlignment = NSTextAlignmentCenter;
topLabel.myLeftMargin = topLabel.myRightMargin = 10;
[topLabel sizeToFit];
[navigationItemLayout addSubview:topLabel];
UILabel *bottomLabel = [UILabel new];
bottomLabel.text = @"线性布局-子视图停靠";
[bottomLabel sizeToFit];
[navigationItemLayout addSubview:bottomLabel];
self.navigationItem.titleView = navigationItemLayout;
}
-(void)handleNavTitleDefault:(id)sender
{
UILabel *topLabel = [UILabel new];
topLabel.text = @"标题的在具有左边按钮和右边按钮时都可以居中";
topLabel.adjustsFontSizeToFitWidth = YES;
topLabel.textAlignment = NSTextAlignmentCenter;
[topLabel sizeToFit];
self.navigationItem.titleView = topLabel;
}
/*
#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.
}
*/
@end
You can’t perform that action at this time.
