出現錯誤的地方,加入key就能夠在衝突的時候輸出相應的信息。html
MASAttachKeys(greenView, redView, blueView, superview); [blueView mas_makeConstraints:^(MASConstraintMaker *make) { //you can also attach debug keys to constaints make.edges.equalTo(@1).key(@"ConflictingConstraint"); //composite constraint keys will be indexed make.height.greaterThanOrEqualTo(@5000).key(@"ConstantConstraint"); make.top.equalTo(greenView.mas_bottom).offset(padding); make.left.equalTo(superview.mas_left).offset(padding); make.bottom.equalTo(superview.mas_bottom).offset(-padding).key(@"BottomConstraint"); make.right.equalTo(superview.mas_right).offset(-padding); make.height.equalTo(greenView.mas_height); make.height.equalTo(redView.mas_height).key(@340954); //anything can be a key }];
對視圖進行初始化以後就能夠開始進行約束的設置ios
#import "MASExampleBasicView.h" @implementation MASExampleBasicView - (id)init { self = [super init]; if (!self) return nil; UIView *greenView = UIView.new; greenView.backgroundColor = UIColor.greenColor; greenView.layer.borderColor = UIColor.blackColor.CGColor; greenView.layer.borderWidth = 2; [self addSubview:greenView]; UIView *redView = UIView.new; redView.backgroundColor = UIColor.redColor; redView.layer.borderColor = UIColor.blackColor.CGColor; redView.layer.borderWidth = 2; [self addSubview:redView]; UIView *blueView = UIView.new; blueView.backgroundColor = UIColor.blueColor; blueView.layer.borderColor = UIColor.blackColor.CGColor; blueView.layer.borderWidth = 2; [self addSubview:blueView]; UIView *superview = self; int padding = 10; //if you want to use Masonry without the mas_ prefix //define MAS_SHORTHAND before importing Masonry.h see Masonry iOS Examples-Prefix.pch [greenView makeConstraints:^(MASConstraintMaker *make) { make.top.greaterThanOrEqualTo(superview.top).offset(padding); make.left.equalTo(superview.left).offset(padding); make.bottom.equalTo(blueView.top).offset(-padding); make.right.equalTo(redView.left).offset(-padding); make.width.equalTo(redView.width); make.height.equalTo(redView.height); make.height.equalTo(blueView.height); }]; //with is semantic and option [redView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(superview.mas_top).with.offset(padding); //with with make.left.equalTo(greenView.mas_right).offset(padding); //without with make.bottom.equalTo(blueView.mas_top).offset(-padding); make.right.equalTo(superview.mas_right).offset(-padding); make.width.equalTo(greenView.mas_width); make.height.equalTo(@[greenView, blueView]); //can pass array of views }]; [blueView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(greenView.mas_bottom).offset(padding); make.left.equalTo(superview.mas_left).offset(padding); make.bottom.equalTo(superview.mas_bottom).offset(-padding); make.right.equalTo(superview.mas_right).offset(-padding); make.height.equalTo(@[greenView.mas_height, redView.mas_height]); //can pass array of attributes }]; return self; } @end
對center和size也能夠進行約束git
[orangeView mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(CGPointMake(0, 50)); make.size.equalTo(CGSizeMake(200, 100)); }];
在某個視圖的基礎上進行變化github
[view mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(lastView).insets(UIEdgeInsetsMake(5, 10, 15, 20)); }];
優先級和非相等的位置變化關係app
lessThanOrEqualToless
priorityLow()dom
[self.topInnerView mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(self.topInnerView.mas_height).multipliedBy(3); make.width.and.height.lessThanOrEqualTo(self.topView); make.width.and.height.equalTo(self.topView).with.priorityLow(); make.center.equalTo(self.topView); }];
在UITableViewCell中設置動畫
更新約束this
動畫spa
UIScrollView中寫約束,須要指定好各個UIView和scrollView的關係,並指明各個UIView的尺寸
- (void)generateContent { UIView* contentView = UIView.new; [self.scrollView addSubview:contentView]; [contentView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.scrollView); make.width.equalTo(self.scrollView); }]; UIView *lastView; CGFloat height = 25; for (int i = 0; i < 10; i++) { UIView *view = UIView.new; view.backgroundColor = [self randomColor]; [contentView addSubview:view]; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)]; [view addGestureRecognizer:singleTap]; [view mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(lastView ? lastView.bottom : @0); make.left.equalTo(@0); make.width.equalTo(contentView.width); make.height.equalTo(@(height)); }]; height += 25; lastView = view; } [contentView makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(lastView.bottom); }]; }
以動畫的形式更新約束
- (void)updateConstraints { [self.growingButton updateConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self); make.width.equalTo(@(self.buttonSize.width)).priorityLow(); make.height.equalTo(@(self.buttonSize.height)).priorityLow(); make.width.lessThanOrEqualTo(self); make.height.lessThanOrEqualTo(self); }]; //according to apple super should be called at end of method [super updateConstraints]; } - (void)didTapGrowButton:(UIButton *)button { self.buttonSize = CGSizeMake(self.buttonSize.width * 1.3, self.buttonSize.height * 1.3); [self setNeedsUpdateConstraints]; [UIView animateWithDuration:3 animations:^{ [self layoutIfNeeded]; }]; }
以動畫形式 remake約束
// this is Apple's recommended place for adding/updating constraints - (void)updateConstraints { [self.movingButton remakeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(@(100)); make.height.equalTo(@(100)); if (self.topLeft) { make.left.equalTo(self.left).with.offset(10); make.top.equalTo(self.top).with.offset(10); } else { make.bottom.equalTo(self.bottom).with.offset(-10); make.right.equalTo(self.right).with.offset(-10); } }]; //according to apple super should be called at end of method [super updateConstraints]; } - (void)toggleButtonPosition { self.topLeft = !self.topLeft; // tell constraints they need updating [self setNeedsUpdateConstraints]; // update constraints now so we can animate the change [self updateConstraintsIfNeeded]; [UIView animateWithDuration:0.4 animations:^{ [self layoutIfNeeded]; }]; } @end
可變高度的TableViewCell實踐(該部份內容只是思路的總結,參考的是stackflow 和cocoachina中的思路)
設置UITableView自動調整高度,而後對cell的約束須要相似UIScrollView中的思路,既要對ContentView作高度方向的約束,也要對自身的高度進行設定。由於cell的高度很大程度上是基於數據模型,所以在cell的updateConstraints中進行更新是比較好的一個選擇。
Masonry項目地址
https://github.com/SnapKit/Masonry
動態高度的參考文章
http://www.cocoachina.com/ios/20160330/15819.html