作者:Mitchell
一搔耕、代碼添加約束順序###
- 創(chuàng)建控件
- 將控件添加到父控件中
- 關(guān)閉需要添加約束的控件的Autoresizing屬性
- 創(chuàng)建約束
二、注意約束添加到哪里###
- 如果是自身的屬性囚聚,如寬高,添加到自身
- 如果是關(guān)于父視圖的約束,添加到父視圖
- 如果是兩個(gè)控件珍语,添加到兩個(gè)控件共同的父視圖。
三竖幔、需求案例###
// 1.創(chuàng)建一個(gè)控件
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
// 1.如果是通過(guò)代碼添加Autolayout, 那么必須在添加約束之前禁用Autoresizing
// 2.禁用Autoresizing時(shí), 必須是給誰(shuí)添加就禁用誰(shuí)的, 也就是說(shuō)如果禁用了父控件無(wú)效
// 3.添加約束之前, 必須保證被約束的控件已經(jīng)添加到父控件中了
// self.view.translatesAutoresizingMaskIntoConstraints = NO;
redView.translatesAutoresizingMaskIntoConstraints = NO;
// 2.創(chuàng)建約束對(duì)象
/*
Item == 第一個(gè)控件
attribute == 第一個(gè)控件的什么屬性
relatedBy == 等于/小于等于/大于等于
toItem == 第二個(gè)控件
attribute == 第二個(gè)控件的什么屬性
multiplier == 乘以多少
constant == 加上多少
*/
// 2.1頂部約束
NSLayoutConstraint *topCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20];
[self.view addConstraint:topCos];
// 2.1左邊約束
NSLayoutConstraint *leftCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
[self.view addConstraint:leftCos];
// 2.1底部約束
NSLayoutConstraint *bottomCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-20];
[self.view addConstraint:bottomCos];
// 2.1右邊約束
NSLayoutConstraint *rightCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
[self.view addConstraint:rightCos];
// 寫(xiě)在這里不行, 必須在設(shè)置約束之前添加
// [self.view addSubview:redView];
// 1.創(chuàng)建兩個(gè)控件
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
// 2.將兩個(gè)控件添加到父控件中
[self.view addSubview:redView];
[self.view addSubview:blueView];
// 3.關(guān)閉需要添加約束的控件的Autoresizing屬性
redView.translatesAutoresizingMaskIntoConstraints = NO;
blueView.translatesAutoresizingMaskIntoConstraints = NO;
// 4.創(chuàng)建約束
// 4.1藍(lán)色約束
// 4.1.1頂部約束
NSLayoutConstraint *blueTopCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20];
[self.view addConstraint:blueTopCos];
// 4.1.2左邊約束
NSLayoutConstraint *blueLeftCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
[self.view addConstraint:blueLeftCos];
// 4.1.3右邊約束
NSLayoutConstraint *blueRightCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
[self.view addConstraint:blueRightCos];
// 4.1.4高度約束
NSLayoutConstraint *blueHeightCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:kNilOptions multiplier:1.0 constant:50];
[blueView addConstraint:blueHeightCos];
// 4.2紅色約束
// 4.2.1頂部約束
NSLayoutConstraint *redTopCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
[self.view addConstraint:redTopCos];
// 4.2.2右邊約束
NSLayoutConstraint *redRightCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
[self.view addConstraint:redRightCos];
// 4.2.3高度約束
NSLayoutConstraint *redHeightCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0];
[self.view addConstraint:redHeightCos];
// 4.2.4寬度約束
NSLayoutConstraint *redWidthCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0];
[self.view addConstraint:redWidthCos];
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者