15 autoLayout使用

按比例設(shè)置寬高,間距大小的問題

1.如果某個視圖是根據(jù)父視圖的大小而定,那么可以把按著control鍵,拉一條線到它的父視圖上面,選擇一個Equia Widths,或者Equia Heights,前者是寬度隨著父視圖的大小而定,后者是高度隨著父視圖而定。

2.如果要做兩個正方形并排放,這時候可以利用它本身自帶的aspact ratio功能,讓其高寬比例是1:1。按著control鍵拉一條線指向它自身,這時候就會出現(xiàn)這個選項。

Horizontal Center in Container —> 水平居中約束

Vertical Center in Container —> 垂直居中約束

上面4根線的作用設(shè)置距離父控件上下左右距離多少

下面width、height 設(shè)置控件自己的大小

托線設(shè)置約束

父子之間控件

Leading Space to Container Margin —> Leading Space to Container 距離父控件左邊的間距

Trailing Space to Container Margin == Trailing Space to Container —> 距離父控件右邊邊的間距

Bottom Space to Container

Bottom Space to Container —> 距離父控件下邊的間距

Center Horizontally in Container —> 與父控件水平居中

Center Vertical in Container —> 與父控件垂直居中

兄弟之間控件

Horizontal Spacing —> 相對于兄弟水平間距

Vertical Spacing —> 相對于兄弟豎直間距

Top —> 相對于兄弟上邊距

Center Vertically —> 相對于兄弟垂直中心對齊

Baseline —> 文字距UI控件頂部的偏移量

Bottom —> 相對于兄弟下邊距

Leading \ Left —> 相對于兄弟左邊距

Center Horizontally —> 相對于兄弟水平中心對齊

Trailing \ Right —> 相對于兄弟右邊距

Equal Widths —> 相對于兄弟寬度

Equal Heights —> 相對于兄弟高度

Aspect Ratio —>

Hold Shift To select multiple —> 按住Shift可以選擇多個

Hold Option for alternates —> 按住Option可以替換(不好用)

代碼設(shè)置約束

約束添加到who上

如果這個約束只與自己有關(guān),那么添加到自己身上

如果這個約束自己與父控件有關(guān),那么添加到父控件上

如果這個約束自己與其他控件有關(guān)系,那么添加到他們公共父輩的控件上

NslayoutConstraint *constraint = [NSlayoutConstraint constraintWithItem:view1 attribute:attr1 relatedBy:relation toItem:view2 attribute: attr2 multiplier:multiplier constant:c];

view1 : 要約束的view

attr1 : 那個屬性(x, y, width, height)

NSLayoutAttributeLeft —> 左

NSLayoutAttributeRight —> 右

NSLayoutAttributeTop —> 上

NSLayoutAttributeBottom —> 下

NSLayoutAttributeLeading —> 左

NSLayoutAttributeTrailing —> 右

NSLayoutAttributeWidth —> 寬度

NSLayoutAttributeHeight —> 高度

NSLayoutAttributeCenterX —> 中心點(diǎn)X

NSLayoutAttributeCenterY —> 中心點(diǎn)Y

NSLayoutAttributeBaseline —> 基本線

NSLayoutAttributeLastBaseline —> 同 NSLayoutAttributeBaseline

NSLayoutAttributeFirstBaseline —> 第一條基本線

iOS8 新增約束

NSLayoutAttributeLeftMargin —> 左邊距

NSLayoutAttributeRightMargin —> 右邊距

NSLayoutAttributeTopMargin —> 上邊距

NSLayoutAttributeBottomMargin —> 下邊距

NSLayoutAttributeLeadingMargin —> 左邊距

NSLayoutAttributeTrailingMargin —> 右邊距

NSLayoutAttributeCenterXWithinMargins —> 中心X邊距

NSLayoutAttributeCenterYWithinMargins —> 中心Y邊距

NSLayoutAttributeNotAnAttribute —> 沒有邊距

relation : 約束關(guān)系

NSLayoutRelationLessThanOrEqual <=

NSLayoutRelationEqual ==

NSLayoutRelationGreaterThanOrEqual >=

view2 : 參照的view

attr2 : 參照的屬性(x, y, width, height)

multiplier : 倍數(shù)關(guān)系

c : 額外的值, 在倍數(shù)之外的值

view1.attr1 relation(= or < or >) view2.attr2 * multiplier(倍數(shù)) + c

注意:1.一定先有父控件,再添加約束,否則會報錯誤.

// 使用約束一定要設(shè)置這個值,禁止系統(tǒng)把AutoresizingMask的一些設(shè)置轉(zhuǎn)換成Autolayout

2. self.view1.translatesAutoresizingMaskIntoConstraints = NO;

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

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

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