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

? 如果一個(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)拣凹,用起來還是很方便的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末恨豁,一起剝皮案震驚了整個(gè)濱河市嚣镜,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌橘蜜,老刑警劉巖菊匿,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異计福,居然都是意外死亡跌捆,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進(jìn)店門象颖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來佩厚,“玉大人,你說我怎么就攤上這事说订〕撸” “怎么了?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵陶冷,是天一觀的道長钙姊。 經(jīng)常有香客問我,道長埂伦,這世上最難降的妖魔是什么煞额? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上膊毁,老公的妹妹穿的比我還像新娘胀莹。我一直安慰自己,他們只是感情好媚媒,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布嗜逻。 她就那樣靜靜地躺著,像睡著了一般缭召。 火紅的嫁衣襯著肌膚如雪栈顷。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天嵌巷,我揣著相機(jī)與錄音萄凤,去河邊找鬼。 笑死搪哪,一個(gè)胖子當(dāng)著我的面吹牛靡努,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播晓折,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼惑朦,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了漓概?” 一聲冷哼從身側(cè)響起漾月,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎胃珍,沒想到半個(gè)月后梁肿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡觅彰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年吩蔑,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片填抬。...
    茶點(diǎn)故事閱讀 40,030評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡烛芬,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出飒责,到底是詐尸還是另有隱情赘娄,我是刑警寧澤,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布读拆,位于F島的核電站,受9級(jí)特大地震影響鸵闪,放射性物質(zhì)發(fā)生泄漏檐晕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望辟灰。 院中可真熱鬧个榕,春花似錦、人聲如沸芥喇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽继控。三九已至械馆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間武通,已是汗流浹背霹崎。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留冶忱,地道東北人尾菇。 一個(gè)月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像囚枪,于是被迫代替她去往敵國和親派诬。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容