// 系統的約束代碼
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *superView = self.view;
UIView *viewDemo = [[UIView alloc] init];
viewDemo.translatesAutoresizingMaskIntoConstraints = NO;
viewDemo.backgroundColor = [UIColor orangeColor];
[superView addSubview:viewDemo];
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[superView addConstraints:@[
// viewDemo頂部距離父視圖superView頂部距離為padding.top(即為:10),
[NSLayoutConstraint constraintWithItem:viewDemo attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1 constant:padding.top],
[NSLayoutConstraint constraintWithItem:viewDemo attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1 constant:padding.left],
[NSLayoutConstraint constraintWithItem:viewDemo attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1 constant:-padding.right],
[NSLayoutConstraint constraintWithItem:viewDemo attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1 constant:-padding.bottom]
]];
}
// 注:若是把右邊約束去掉,改為如下語句
// [NSLayoutConstraint constraintWithItem:viewDemo attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0]
// 則viewDemo的寬度變為superView的0.5倍?。?/span>
//***************************************************
// VFL語言
UIView *viewLeft = [[UIView alloc] init];
viewLeft.translatesAutoresizingMaskIntoConstraints = NO;
viewLeft.backgroundColor = [UIColor orangeColor];
[self.view addSubview:viewLeft];
UIView *viewRight = [[UIView alloc] init];
viewRight.translatesAutoresizingMaskIntoConstraints = NO;
viewRight.backgroundColor = [UIColor redColor];
[self.view addSubview:viewRight];
// horizontal:水平方向距左邊10,距右邊10
//簡單說來,NSDictionaryOfVariableBindings(scrollView)就等于@{@”scrollView”: scrollView}
NSDictionary *constraintDict = NSDictionaryOfVariableBindings(viewLeft,viewRight);
NSArray *hConstraintArrayLeft = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10)-[viewLeft]-(10)-|" options:0 metrics:nil views:constraintDict];
// vertical: 垂直方向距頂部30,控件高度100
NSArray *vConstraintArrayLeft = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(30)-[viewLeft(100)]" options:0 metrics:nil views:constraintDict];
// 添加到父視圖上
[self.view addConstraints:hConstraintArrayLeft];
[self.view addConstraints:vConstraintArrayLeft];
NSArray *hConstraintArrayRight = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10)-[viewRight]-(10)-|" options:0 metrics:nil views:constraintDict];
// viewRight距viewLeft的垂直距離為50
NSArray *vConstraintArrayRight = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[viewLeft]-(50)-[viewRight(200)]" options:0 metrics:nil views:constraintDict];
[self.view addConstraints:hConstraintArrayRight];
[self.view addConstraints:vConstraintArrayRight];
//***************************************************
// 三方庫:Masonry
// 居中
// 快速定義一個weakSelf,用于block里面,防止循環引用
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self
WS(ws);
UIView *sv = [UIView new];
sv.backgroundColor = [UIColor orangeColor];
// 在做autoLayout之前 一定要先將view添加到superView上 否則會報錯
[self.view addSubview:sv];
[sv mas_makeConstraints:^(MASConstraintMaker *make) {
// 居中
make.center.equalTo(ws.view);
// 設置size
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
3. [初級] 讓兩個高度為150的view垂直居中且等寬且等間隔排列 間隔為10(自動計算其寬度)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | int padding1 = 10; [sv2 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(sv.mas_centerY); make.left.equalTo(sv.mas_left). with .offset(padding1); make.right.equalTo(sv3.mas_left). with .offset(-padding1); make.height.mas_equalTo(@150); make.width.equalTo(sv3); }]; [sv3 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(sv.mas_centerY); make.left.equalTo(sv2.mas_right). with .offset(padding1); make.right.equalTo(sv.mas_right). with .offset(-padding1); make.height.mas_equalTo(@150); make.width.equalTo(sv2); }]; |
代碼效果
這里我們在兩個子view之間互相設置的約束 可以看到他們的寬度在約束下自動的被計算出來了
4. [中級] 在UIScrollView順序排列一些view并自動計算contentSize
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 | UIScrollView *scrollView = [UIScrollView new ]; scrollView.backgroundColor = [UIColor whiteColor]; [sv addSubview:scrollView]; [scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(sv). with .insets(UIEdgeInsetsMake(5,5,5,5)); }]; UIView *container = [UIView new ]; [scrollView addSubview:container]; [container mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(scrollView); make.width.equalTo(scrollView); }]; int count = 10; UIView *lastView = nil; for ( int i = 1 ; i <= count ; ++i ) { UIView *subv = [UIView new ]; [container addSubview:subv]; subv.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 ) saturation:( arc4random() % 128 / 256.0 ) + 0.5 brightness:( arc4random() % 128 / 256.0 ) + 0.5 alpha:1]; [subv mas_makeConstraints:^(MASConstraintMaker *make) { make.left.and.right.equalTo(container); make.height.mas_equalTo(@(20*i)); if ( lastView ) { make.top.mas_equalTo(lastView.mas_bottom); } else { make.top.mas_equalTo(container.mas_top); } }]; lastView = subv; } [container mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(lastView.mas_bottom); }]; |
頭部效果
尾部效果
從scrollView的scrollIndicator可以看出 scrollView的內部已如我們所想排列好了
這里的關鍵就在于container這個view起到了一個中間層的作用 能夠自動的計算uiscrollView的contentSize
5. [高級] 橫向或者縱向等間隙的排列一組view
很遺憾 autoLayout并沒有直接提供等間隙排列的方法(Masonry的官方demo中也沒有對應的案例) 但是參考案例3 我們可以通過一個小技巧來實現這個目的 為此我寫了一個Category
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 | @implementation UIView(Masonry_LJC) - (void) distributeSpacingHorizontallyWith:(NSArray*)views { NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1]; for ( int i = 0 ; i < views.count+1 ; ++i ) { UIView *v = [UIView new ]; [spaces addObject:v]; [self addSubview:v]; [v mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(v.mas_height); }]; } UIView *v0 = spaces[0]; __weak __typeof(&*self)ws = self; [v0 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(ws.mas_left); make.centerY.equalTo(((UIView*)views[0]).mas_centerY); }]; UIView *lastSpace = v0; for ( int i = 0 ; i < views.count; ++i ) { UIView *obj = views[i]; UIView *space = spaces[i+1]; [obj mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(lastSpace.mas_right); }]; [space mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(obj.mas_right); make.centerY.equalTo(obj.mas_centerY); make.width.equalTo(v0); }]; lastSpace = space; } [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(ws.mas_right); }]; } - (void) distributeSpacingVerticallyWith:(NSArray*)views { NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1]; for ( int i = 0 ; i < views.count+1 ; ++i ) { UIView *v = [UIView new ]; [spaces addObject:v]; [self addSubview:v]; [v mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(v.mas_height); }]; } UIView *v0 = spaces[0]; __weak __typeof(&*self)ws = self; [v0 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(ws.mas_top); make.centerX.equalTo(((UIView*)views[0]).mas_centerX); }]; UIView *lastSpace = v0; for ( int i = 0 ; i < views.count; ++i ) { UIView *obj = views[i]; UIView *space = spaces[i+1]; [obj mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(lastSpace.mas_bottom); }]; [space mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(obj.mas_bottom); make.centerX.equalTo(obj.mas_centerX); make.height.equalTo(v0); }]; lastSpace = space; } [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(ws.mas_bottom); }]; } @end |
簡單的來測試一下
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 | UIView *sv11 = [UIView new ]; UIView *sv12 = [UIView new ]; UIView *sv13 = [UIView new ]; UIView *sv21 = [UIView new ]; UIView *sv31 = [UIView new ]; sv11.backgroundColor = [UIColor redColor]; sv12.backgroundColor = [UIColor redColor]; sv13.backgroundColor = [UIColor redColor]; sv21.backgroundColor = [UIColor redColor]; sv31.backgroundColor = [UIColor redColor]; [sv addSubview:sv11]; [sv addSubview:sv12]; [sv addSubview:sv13]; [sv addSubview:sv21]; [sv addSubview:sv31]; //給予不同的大小 測試效果 [sv11 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(@[sv12,sv13]); make.centerX.equalTo(@[sv21,sv31]); make.size.mas_equalTo(CGSizeMake(40, 40)); }]; [sv12 mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(70, 20)); }]; [sv13 mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(50, 50)); }]; [sv21 mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(50, 20)); }]; [sv31 mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(40, 60)); }]; [sv distributeSpacingHorizontallyWith:@[sv11,sv12,sv13]]; [sv distributeSpacingVerticallyWith:@[sv11,sv21,sv31]]; [sv showPlaceHolderWithAllSubviews]; [sv hidePlaceHolder]; |
代碼效果
perfect! 簡潔明了的達到了我們所要的效果
這里所用的技巧就是 使用空白的占位view來填充我們目標view的旁邊 這點通過圖上的空白標注可以看出來
引自:http://www.cocoachina.com/ios/20141219/10702.html
若有侵權,請告知
新聞熱點
疑難解答