-(void)test1CenterView{ UIView *sv = [UIView new]; sv.backgroundColor = [UIColor grayColor]; //在作autoLayout以前 必定要先將view添加到superview上 不然會報錯 [self.view addSubview:sv]; [self.view addSubview:self.btn]; [self.btn mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));//btn 間距視圖sv 各邊界距離 // make.center.mas_equalTo(sv); }]; //mas_makeConstraints就是Masonry的autolayout添加函數 將所需的約束添加到block中行了 [sv mas_makeConstraints:^(MASConstraintMaker *make) { //將sv居中 make.center.equalTo(self.view); //將size設置成(300,300) make.size.mas_equalTo(CGSizeMake(150, 150)); }]; } -(void)action{ [self.btn mas_updateConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(50, 50));//點擊後更改按鈕大小,約束會相應更改父視圖的大小 }]; }
-(void)test2Offset{ UIView *sv = [UIView new]; sv.backgroundColor = [UIColor grayColor]; //在作autoLayout以前 必定要先將view添加到superview上 不然會報錯 [self.view addSubview:sv]; [self.view addSubview:self.btn]; [self.btn mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));//btn 間距視圖sv 各邊界距離 // make.center.mas_equalTo(sv); }]; //mas_makeConstraints就是Masonry的autolayout添加函數 將所需的約束添加到block中行了 [sv mas_makeConstraints:^(MASConstraintMaker *make) { //將sv居中 make.center.equalTo(self.view); make.size.mas_equalTo(CGSizeMake(150, 150)); }]; UIView *sv1 = [UIView new]; sv1.backgroundColor = [UIColor greenColor]; [self.view addSubview:sv1]; [sv1 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(sv.mas_bottom).with.offset(10);//底部 10 make.left.equalTo(self.view.mas_left).with.offset(10);//左邊 10 make.size.mas_equalTo(CGSizeMake(100, 100));//大小 }]; UIView *sv2 = [UIView new]; sv2.backgroundColor = [UIColor blueColor]; [self.view addSubview:sv2]; [sv2 mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(sv.mas_top).with.offset(-10); make.right.equalTo(self.view.mas_right).with.offset(-10); make.size.mas_equalTo(CGSizeMake(50, 50)); }]; }
-(void)testScrollview{ //自動佈局設置contentSize UIScrollView *scrollView = [UIScrollView new]; scrollView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:scrollView]; [scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(44,5,5,5)); }]; UIView *container = [UIView new];//中間層 來計算contentSize [scrollView addSubview:container]; [container mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(scrollView); make.width.equalTo(scrollView); }]; int count = 10; UIView *lastView = nil; for ( int i = 1 ; i <= count ; ++i ) { UIView *subv = [UIView new]; [container addSubview:subv]; subv.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 ) saturation:( arc4random() % 128 / 256.0 ) + 0.5 brightness:( arc4random() % 128 / 256.0 ) + 0.5 alpha:1]; [subv mas_makeConstraints:^(MASConstraintMaker *make) { make.left.and.right.equalTo(container); make.height.mas_equalTo(@(20*i)); if ( lastView ) { make.top.mas_equalTo(lastView.mas_bottom); } else { make.top.mas_equalTo(container.mas_top); } }]; lastView = subv; } [container mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(lastView.mas_bottom); }]; }
經過指示器,能夠看到contentSize 已經計算好。ios
參考文章:http://adad184.com/2014/09/28/use-masonry-to-quick-solve-autolayout/dom