以前咱們在屏幕適配的章節中學習過AutoLayout
的使用,但那都是在可視化界面上進行添加約束完成的,咱們不少時候都須要在代碼中使用AutoLayout
約束,蘋果也爲咱們提供了實現,使用NSLayoutConstraint
類表示約束,但使用起來比較複雜,代碼量比較大,例如建立一個約束的方法:數組
+ (id)constraintWithItem:(id)view1 /* 一個UIView */ attribute:(NSLayoutAttribute)attribute1 /* 屬性 */ relatedBy:(NSLayoutRelation)relation /* 關係 */ toItem:(id)view2 /* 另外一個UIView */ attribute:(NSLayoutAttribute)attribute2 /* 屬性 */ multiplier:(CGFloat)multiplier /* 倍數 */ constant:(CGFloat)constant; /* 偏移 */
若是約束一多,這個方法調用次數就會越多,代碼就會變得很長。
實際上咱們能夠使用第三方框架Masonry
,該框架是一個輕量級的佈局框架,封裝了AutoLayout
,擁有本身的描述語法,採用更優雅的鏈式語法,簡潔明瞭,並具備高可讀性。框架
Masonry
基本支持AutoLayout
的全部屬性:@property (nonatomic, strong, readonly) MASConstraint *left; @property (nonatomic, strong, readonly) MASConstraint *top; @property (nonatomic, strong, readonly) MASConstraint *right; @property (nonatomic, strong, readonly) MASConstraint *bottom; @property (nonatomic, strong, readonly) MASConstraint *leading; @property (nonatomic, strong, readonly) MASConstraint *trailing; @property (nonatomic, strong, readonly) MASConstraint *width; @property (nonatomic, strong, readonly) MASConstraint *height; @property (nonatomic, strong, readonly) MASConstraint *centerX; @property (nonatomic, strong, readonly) MASConstraint *centerY; @property (nonatomic, strong, readonly) MASConstraint *baseline;
NSLayoutAttrubute
的對照表以下:Masonry
的大部分方法都爲UIView
實現了分類,使咱們能夠十分簡單的使用less
/* 添加新約束,只負責新增約束,不能同時存在兩條針對於同一對象的約束,不然會報錯 */ - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; /* 更新原有的約束,針對上面的狀況,會更新在block中出現的約束,不會致使出現兩個相同約束的狀況 */ - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block; /* 刪除以前約束,從新添加約束,會清除以前的全部約束,僅保留最新的約束 */ - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
- 上面三個方法不只只有
UIView
能夠調用,存有UIView
的NSArray
數組也能夠調用,表示遍歷數組中全部UIView
進行調用
/* 屬性attribute能夠連用,好比top.right.bottom.left 加mas_前綴和沒有mas_前綴效果差很少,只是加mas_前綴會對參數裝箱, 參數有結構體的時候,須要使用mas_前綴,沒有mas_前綴的參數必須爲對象 */ /* 數值約束,top對應NSInteger,center對應NSPoint,size對應NSSize */ make.[attribute].mas_equalTo([value]); /* 等於約束,兩個view之間比較,注意:沒有other.edges的屬性,edges對應otherView */ make.[attribute].equalTo(otherView.[attribute]); /* 大於等於約束,兩個view之間比較 */ make.[attribute].greaterThanOrEqualTo(otherView.[attribute]); /* 小於等於約束,兩個view之間比較 */ make.[attribute].lessThanOrEqualTo(otherView.[attribute]); /* 偏移約束 */ make.[attribute].equalTo(otherView.[attribute]).offset([value]); /* 邊界約束,有上、下、左、右邊界 */ make.edges.equalTo(otherView).insets(UIEdgeInsetsMake([topValue],[leftValue],[bottomValue],[rightValue])); /* Margin約束 */ make.[attribute].equalTo(otherView.[attribute]Margin);
添加約束前,必須先把
UIView
添加到父視圖中,不然會閃退dom
- (void)viewDidLoad { [super viewDidLoad]; UIView *sv = [[UIView alloc] init]; sv.backgroundColor = [UIColor blackColor]; //必定要先將view添加到superView上,不然會出錯 [self.view addSubview:sv]; //Masonry的autolayout添加約束 __weak ViewController *weakSelf = self; [sv mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(weakSelf.view);//將sv居中 make.size.mas_equalTo(CGSizeMake(300, 300));//將sv的尺寸設置爲(300,300) }]; }
UIView *sv = [[UIView alloc] init]; sv.backgroundColor = [UIColor blackColor]; //必定要先將view添加到superView上,不然會出錯 [self.view addSubview:sv]; //Masonry的autolayout添加約束 __weak ViewController *weakSelf = self; [sv mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(weakSelf.view);//將sv居中 make.size.mas_equalTo(CGSizeMake(300, 300));//將sv的尺寸設置爲(300,300) }]; UIView *sv1 = [[UIView alloc] init]; sv1.backgroundColor = [UIColor redColor]; //必定要先將view添加到superView上,不然會出錯 [sv addSubview:sv1]; //Masonry的autolayout添加約束 [sv1 mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10)); /* 等價於 make.top.equalTo(sv).with.offset(10); make.left.equalTo(sv).with.offset(10); make.bottom.equalTo(sv).with.offset(-10); make.right.equalTo(sv).with.offset(-10); */ /* 也等價於 make.top.left.bottom.and.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10)); */ }];
- (MASConstraint *)with { return self; } - (MASConstraint *)and { return self; }
UIView *sv = [[UIView alloc] init]; sv.backgroundColor = [UIColor blackColor]; //必定要先將view添加到superView上,不然會出錯 [self.view addSubview:sv]; //Masonry的autolayout添加約束 __weak ViewController *weakSelf = self; [sv mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(weakSelf.view);//將sv居中 make.size.mas_equalTo(CGSizeMake(300, 300));//將sv的尺寸設置爲(300,300) }]; int padding1 = 10; UIView *leftView = [[UIView alloc] init]; leftView.backgroundColor = [UIColor orangeColor]; [sv addSubview:leftView]; UIView *rightView = [[UIView alloc] init]; rightView.backgroundColor = [UIColor orangeColor]; [sv addSubview:rightView]; //Masonry的autolayout添加約束 [leftView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(sv.mas_centerY);//中心Y軸和sv中心Y軸相等 make.left.equalTo(sv.mas_left).with.offset(padding1);//左邊距離sv的左邊界10 make.right.equalTo(rightView.mas_left).with.offset(-padding1);//右邊距離rightView的左邊界-10 make.height.mas_equalTo(@150);//高度150 make.width.equalTo(rightView);//寬度等於rightView }]; [rightView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(sv.mas_centerY);//中心Y軸和sv中心Y軸相等 make.left.equalTo(leftView.mas_right).with.offset(padding1);//左邊距離leftView的右邊界10 make.right.equalTo(sv.mas_right).with.offset(-padding1);//右邊距離sv的右邊界-10 make.height.mas_equalTo(@150);//高度150 make.width.equalTo(leftView);//寬度等於leftView }];
UIView *sv = [[UIView alloc] init]; sv.backgroundColor = [UIColor blackColor]; //必定要先將view添加到superView上,不然會出錯 [self.view addSubview:sv]; //Masonry的autolayout添加約束 __weak ViewController *weakSelf = self; [sv mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(weakSelf.view);//將sv居中 make.size.mas_equalTo(CGSizeMake(300, 300));//將sv的尺寸設置爲(300,300) }]; /* 建立ScrollView */ UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.backgroundColor = [UIColor whiteColor]; [sv addSubView:scrollView]; [scrollView mas_makeConstraints:^(MASConstraintMaker *make) { //設置邊界約束 make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(5,5,5,5)); }]; //建立ScrollView子視圖容器視圖 UIView *container = [[UIView alloc] init]; [scrollView addSubView:container]; //添加container約束 [container mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(scrollView);//邊界緊貼ScrollView邊界 make.width.equalTo(scrollView);//寬度和ScrollView相等 }]; //向container添加多個View int count = 10; UIView *lastView = nil; for(int i = 1;i <= count;++i ){ //建立一個View UIView *subView = [[UIView alloc] init]; [container addSubView:subView]; //顏色隨機 subView.backgroundColor = [UIColor colorWithRed:( arc4random() % 256 / 256.0 ) green:( arc4random() % 256 / 256.0 ) blue:( arc4random() % 256 / 256.0 ) alpha:1]; //向subView添加約束 [subView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.and.right.equalTo(container);//左右邊界和container緊貼 make.height.mas_equalTo(@(20*i));//高度隨i遞增 //判斷是否有前一個子View if ( lastView ) { //若是有前一個View,上邊界和前一個View的下邊界緊貼 make.top.mas_equalTo(lastView.mas_bottom); } else { //若是沒有前一個View,上邊界和container的下邊界緊貼 make.top.mas_equalTo(container.mas_top); } }]; //保存前一個View lastView = subView; } //添加container的最後一個約束 [container mas_makeConstraints:^(MASConstraintMaker *make) { //container的下邊界和最後一個View的下邊界緊貼 make.bottom.equalTo(lastView.mas_bottom); }];
/* 該方法只有存有UIView的NSArray數組能夠調用,NSArray裏面的UIView必須有共同的spuerView 該方法做用是UIView之間的水平等寬定距約束,或者垂直等高定距約束。 先肯定間距,寬度或高度不肯定,但相等 axisType只有MASAxisTypeHorizontal(水平間距類型)和MASAxisTypeVertical(垂直間距類型) fixedSpacing是UIView兩兩之間的間距大小 leadSpacing是第一個UIView距離superView左(或上)邊界的間距大小 tailSpacing是最後一個UIView距離superView右(或下)邊界的間距大小 數組裏的全部UIView水平寬度相等,或者垂直高度相等 */ - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing; /* 該方法只有存有UIView的NSArray數組能夠調用,NSArray裏面的UIView必須有共同的spuerView 該方法做用是UIView之間的水平定寬等距約束,或者垂直定高等距約束。 先肯定寬度或高度,間距不肯定,但相等 axisType只有MASAxisTypeHorizontal(水平間距類型)和MASAxisTypeVertical(垂直間距類型) fixedSpacing是UIView兩兩之間的間距大小 leadSpacing是第一個UIView距離superView左(或上)邊界的間距大小 tailSpacing是最後一個UIView距離superView右(或下)邊界的間距大小 數組裏的全部UIView間距相等 */ - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing;
/* 設置水平等寬定距 UIView之間水平間距爲20,第一個UIView距離superView左邊6, 最後一個UIView距離superView右邊7,UIView高度爲60,距離頂部40 */ [array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:6 tailSpacing:7]; [array mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@40); make.height.equalTo(@60); }];
/* 設置水平定寬等距 全部UIView的寬度爲20,第一個UIView距離superView左邊6, 最後一個UIView距離superView右邊7,UIView高度爲60,距離頂部40 */ [array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:20 leadSpacing:6 tailSpacing:7]; [array mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@40); make.height.equalTo(@60); }];