NSLayoutConstraint - 系統(tǒng)自動布局(詳解)

? 如果一個界面創(chuàng)建三個View,要求第一行兩個View等高等寬,第三個View與上面兩個View等高,無論是橫屏還是豎屏。那該如何編寫使得View自動布局呢?那就要使用NSLayoutConstraint - 系統(tǒng)自動布局了,核心代碼如下:

- (void)createThreeViews{

//1 創(chuàng)建3個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;

//-----------------自動布局方法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ù)說明:

第一個參數(shù):指定約束左邊的視圖view1

第二個參數(shù):指定view1的屬性attr1

第三個參數(shù):指定左右兩邊的視圖的關(guān)系relation

第四個參數(shù):指定約束右邊的視圖view2

第五個參數(shù):指定view2的屬性attr2

第六個參數(shù):指定一個與view2屬性相乘的乘數(shù)multiplier

第七個參數(shù):指定一個與view2屬性相加的浮點數(shù)constant

依據(jù)的公式是:view1.attr1 = view2.attr2*multiplier +constant

2.NSLayoutAttribute的類型:

NSLayoutAttributeLeft 視圖的左邊

NSLayoutAttributeRight 視圖的右邊

NSLayoutAttributeTop 視圖的上邊

NSLayoutAttributeBottom 視圖的下邊

NSLayoutAttributeLeading 視圖的前邊

NSLayoutAttributeTrailing 視圖的后邊

NSLayoutAttributeWidth 視圖的寬度

NSLayoutAttributeHeight 視圖的高度

NSLayoutAttributeCenterX 視圖的中點的X值

NSLayoutAttributeCenterY 視圖中點的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]];

//-----------------自動布局方法2 ----------------

/*

* 1. 自動布局使用可視化語言:VFL(Visual Format Language)

2. 自動布局的相關(guān)參數(shù)

NSArray *constrArray = [NSLayoutConstraint constraintsWithVisualFormat:@"string類型"

options:

metrics:

views:];

3. 解釋:

a) 第一個參數(shù):使用VFL格式化的字符串,可以參見官方的幫助文檔;

b) 第二個參數(shù):指定VFL中所有對象的布局屬性和方向。舉例:有2個視圖使用VFL進行布局,可以使用NSLayoutFormatAlignAllLeft,就讓兩個視圖左對齊;

c) 第三個參數(shù):度量或者指標(biāo)的字典,字典里面有相關(guān)的鍵值對來控制相關(guān)的度量指標(biāo),通過key獲取;

d) 第四個參數(shù):指定約束的視圖:一個或多個。

4. VFL語言的規(guī)則

a) "H" 表示水平方向,"V"表示垂直方向;

b) "|" 表示superview的邊界;

c) "[]" 表示view,"()"表示尺寸,它們可以多個條件組合,中間使用逗號分隔,舉例:[view(>=70, <=100)];

d) "-" 表示間隙;

e) "@"表示優(yōu)先級。舉例: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é):通過一個小小的編程,使大家初步了解了NSLayoutConstraint的使用規(guī)則和作用。這兩種方式各有特點,本人比較喜歡用第二種,因為感覺代碼較少。在使用NSLayoutConstraint 系統(tǒng)自動布局時,注意的地方很多,細心一點,用起來還是很方便的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容