autoLayout是蘋果極力推薦的一種佈局方式,因爲其可視化的操作和極佳的開發效率而備受推崇。autolayout的功能非常強大,下面就簡單說幾點
到現在爲止,所有的約束都可以用autolayout完成
1.控件等距離均勻分佈佈局
- (void)updateViewConstraints
{
[self autoArrangeBoxWithConstraints:@[self.button1X,
self.button2X,
self.button3X,
self.button4X]
width:self.buttonWidth.constant];
[super updateViewConstraints];
}
- (void)autoArrangeBoxWithConstraints:(NSArray *)constraintArray width:(CGFloat)width
{
CGFloat step = (self.view.frame.size.width - (width * constraintArray.count)) / (constraintArray.count + 1);
for (int i = 0; i < constraintArray.count; i++) {
NSLayoutConstraint *constraint = constraintArray[i];
constraint.constant = step * (i + 1) + width * i;
}
}
2.自動佈局基本功能介紹:
對齊設置:
閱讀起始對齊,結尾對齊,控件相對中心對齊,相對父視圖中心對齊
邊距位置設置,長度,寬度設置
控件之間等寬,等高設置
更新frame 和更新constraint ,上端選擇項是針對選中控件,下端選擇項是針對所有控件
3. 快捷小技巧
按住ctrl鍵拖到另外一個控件上可以建立constraint,
按住alt鍵顯示邊距和寬高
加輔助線 editor ->add horizental guide 拖到外面會刪除
效果預覽
點擊方格,最下端有個preview選項,打開各種尺寸
4.- (CGSize)intrinsicContentSize 此方法會根據內容的尺寸自動調節視圖比例 ,自動執行,無需介入
5. 小原則:
設置constraint與儘量少的控件建立聯繫,方便調節