? 如果一個(gè)界面創(chuàng)建三個(gè)View枷餐,要求第一行兩個(gè)View等高等寬,第三個(gè)View與上面兩個(gè)View等高匙奴,無論是橫屏還是豎屏俗冻。那該如何編寫使得View自動(dòng)布局呢颈将?那就要使用NSLayoutConstraint - 系統(tǒng)自動(dòng)布局了,核心代碼如下:
- (void)createThreeViews{
//1 創(chuàng)建3個(gè)view的對象
UIView *leftView = [[UIView alloc]init];
UIView *rightView = [[UIView alloc]init];
UIView *bottomView = [[UIView alloc]init];
//2 設(shè)置背景顏色
leftView.backgroundColor = [UIColor greenColor];
rightView.backgroundColor = [UIColor purpleColor];
bottomView.backgroundColor = [UIColor orangeColor];
//3 添加視圖上顯示
[self.view addSubview:leftView];
[self.view addSubview:rightView];
[self.view addSubview:bottomView];
//4 關(guān)閉系統(tǒng)的自定義布局
leftView.translatesAutoresizingMaskIntoConstraints = NO;
rightView.translatesAutoresizingMaskIntoConstraints = NO;
bottomView.translatesAutoresizingMaskIntoConstraints = NO;
//-----------------自動(dòng)布局方法1 ----------------
/**
[NSLayoutConstraint constraintWithItem:(id)item
attribute:(NSLayoutAttribute)attribute
relatedBy:(NSLayoutRelation)relation
toItem:(id)otherItem
attribute:(NSLayoutAttribute)otherAttribute
multiplier:(CGFloat)multiplier
constant:(CGFloat)constant]
1.參數(shù)說明:
第一個(gè)參數(shù):指定約束左邊的視圖view1
第二個(gè)參數(shù):指定view1的屬性attr1
第三個(gè)參數(shù):指定左右兩邊的視圖的關(guān)系relation
第四個(gè)參數(shù):指定約束右邊的視圖view2
第五個(gè)參數(shù):指定view2的屬性attr2
第六個(gè)參數(shù):指定一個(gè)與view2屬性相乘的乘數(shù)multiplier
第七個(gè)參數(shù):指定一個(gè)與view2屬性相加的浮點(diǎn)數(shù)constant
依據(jù)的公式是:view1.attr1 = view2.attr2*multiplier +constant
2.NSLayoutAttribute的類型:
NSLayoutAttributeLeft 視圖的左邊
NSLayoutAttributeRight 視圖的右邊
NSLayoutAttributeTop 視圖的上邊
NSLayoutAttributeBottom 視圖的下邊
NSLayoutAttributeLeading 視圖的前邊
NSLayoutAttributeTrailing 視圖的后邊
NSLayoutAttributeWidth 視圖的寬度
NSLayoutAttributeHeight 視圖的高度
NSLayoutAttributeCenterX 視圖的中點(diǎn)的X值
NSLayoutAttributeCenterY 視圖中點(diǎn)的Y值
NSLayoutAttributeBaseline 視圖的基準(zhǔn)線
NSLayoutAttributeNotAnAttribute 無屬性
3.NSLayoutRelation的類型:
NSLayoutRelationLessThanOrEqual 視圖關(guān)系小于或等于
NSLayoutRelationEqual? ? ? 視圖關(guān)系等于
NSLayoutRelationGreaterThanOrEqual? ? ? 視圖關(guān)系大于或等于
**/
//leftView的上 = self.view的上+20
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:leftView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.
constant:20]];
// leftView的左 = self.view的左 + 20
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:leftView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.
constant:20]];
// rightView的上 = leftView的上
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:rightView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:leftView
attribute:NSLayoutAttributeTop
multiplier:1.
constant:0]];
// rightView的左 = leftView的右 + 20
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:rightView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:leftView
attribute:NSLayoutAttributeRight
multiplier:1.
constant:20]];
// rightView的右 = self.view的右 - 20
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:rightView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.
constant:-20]];
// rightView的高 = leftView的高
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:rightView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:leftView
attribute:NSLayoutAttributeHeight
multiplier:1.
constant:0]];
// rightView的寬 = leftView的寬
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:rightView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:leftView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0]];
//bottomView的左 = self.view的左 + 20
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bottomView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1
constant:20]];
//bottomView的右 = self.view的右 - 20
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bottomView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1
constant:-20]];
//bottomView的上 = leftView的下 + 20
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bottomView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:leftView
attribute:NSLayoutAttributeBottom
multiplier:1
constant:20]];
//bottomView的高 = leftView的高
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bottomView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:leftView
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0]];
//bottomView的下 = self.view的下 + 20
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bottomView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:-20]];
//-----------------自動(dòng)布局方法2 ----------------
/*
* 1. 自動(dòng)布局使用可視化語言:VFL(Visual Format Language)
2. 自動(dòng)布局的相關(guān)參數(shù)
NSArray *constrArray = [NSLayoutConstraint constraintsWithVisualFormat:@"string類型"
options:
metrics:
views:]言疗;
3. 解釋:
a) 第一個(gè)參數(shù):使用VFL格式化的字符串,可以參見官方的幫助文檔颂砸;
b) 第二個(gè)參數(shù):指定VFL中所有對象的布局屬性和方向噪奄。舉例:有2個(gè)視圖使用VFL進(jìn)行布局,可以使用NSLayoutFormatAlignAllLeft人乓,就讓兩個(gè)視圖左對齊勤篮;
c) 第三個(gè)參數(shù):度量或者指標(biāo)的字典,字典里面有相關(guān)的鍵值對來控制相關(guān)的度量指標(biāo)色罚,通過key獲扰龅蕖;
d) 第四個(gè)參數(shù):指定約束的視圖:一個(gè)或多個(gè)戳护。
4. VFL語言的規(guī)則
a) "H" 表示水平方向金抡,"V"表示垂直方向瀑焦;
b) "|" 表示superview的邊界;
c) "[]" 表示view梗肝,"()"表示尺寸榛瓮,它們可以多個(gè)條件組合,中間使用逗號(hào)分隔巫击,舉例:[view(>=70, <=100)];
d) "-" 表示間隙禀晓;
e) "@"表示優(yōu)先級(jí)。舉例:V:|-50@750-[view(55)]
*/
/**
//創(chuàng)建VFL約束字符串
NSString *hVFL = @"H:|-space-[leftView(==rightView)]-space1-[rightView]-space-|";
NSString *hVFL1 = @"H:|-space-[bottomView]-space-|";
NSString *vVFL = @"V:|-space-[leftView(==bottomView)]-space-[bottomView]-space-|";
NSString *vVFL1 = @"V:|-space-[rightView(==bottomView)]-space-[bottomView]-space-|";
//創(chuàng)建鍵值映射
NSDictionary *metircs = @{@"space":@20,@"space1":@30};
NSDictionary *views = NSDictionaryOfVariableBindings(leftView,rightView,bottomView);
//創(chuàng)建約束
NSArray *hconstraint = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options:NSLayoutFormatDirectionLeadingToTrailing metrics:metircs views:views];
NSArray *hconstraint1 = [NSLayoutConstraint constraintsWithVisualFormat:hVFL1 options:NSLayoutFormatDirectionLeadingToTrailing metrics:metircs views:views];
NSArray *vconstraint = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options:NSLayoutFormatDirectionLeadingToTrailing metrics:metircs views:views];
NSArray *vconstraint1 = [NSLayoutConstraint constraintsWithVisualFormat:vVFL1 options:NSLayoutFormatDirectionLeadingToTrailing metrics:metircs views:views];
//添加約束
[self.view addConstraints:hconstraint];
[self.view addConstraints:hconstraint1];
[self.view addConstraints:vconstraint];
[self.view addConstraints:vconstraint1];
**/
}
總結(jié):通過一個(gè)小小的編程坝锰,使大家初步了解了NSLayoutConstraint的使用規(guī)則和作用粹懒。這兩種方式各有特點(diǎn),本人比較喜歡用第二種顷级,因?yàn)楦杏X代碼較少凫乖。在使用NSLayoutConstraint 系統(tǒng)自動(dòng)布局時(shí),注意的地方很多愕把,細(xì)心一點(diǎn)拣凹,用起來還是很方便的。