iOS:Masonry练习详解
生活随笔
收集整理的這篇文章主要介紹了
iOS:Masonry练习详解
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Masonry練習(xí)詳解
添加約束的方式:1.通過(guò)使用NSLayoutConstraints添加約束到約束數(shù)組中,之前必須設(shè)置translatesAutoresizingMaskIntoConstraints = NO,即取消自動(dòng)布局; ? 2.通過(guò)使用MASConstraintMaker在block中添加約束,不需要再設(shè)置translatesAutoresizingMaskIntoConstraintst?屬性,block內(nèi)部已經(jīng)幫助完成; 約束的關(guān)系:
equalTo? <=======>? ?NSLayoutRelationEqual ? 等于
lessThanOrEqualTo? ?<======>??NSLayoutRelationLessThanOrEqual ? 小于或等于
greaterThanOrEqualTo?<=======> ?NSLayoutRelationGreaterThanOrEqual ?大于或等于
?
MASViewAttribute:視圖約束屬性
UIView/NSView 這兩個(gè)約束完全相同,都是view左邊大于等于label的左邊位置 make.left.greaterThanOrEqualTo(label); make.left.greaterThanOrEqualTo(label.mas_left); NSNumber給約束設(shè)置具體的值 <1>//width >= 200 && width <= 400 make.width.greaterThanOrEqualTo(@200); make.width.lessThanOrEqualTo(@400) <2>//creates view.left = view.superview.left + 10 make.left.lessThanOrEqualTo(@10) 代替NSNumber,使用原始的數(shù)據(jù)或者結(jié)構(gòu)體設(shè)置約束數(shù)據(jù) make.top.mas_equalTo(42); make.height.mas_equalTo(20); make.size.mas_equalTo(CGSizeMake(50, 100)); make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0)); make.left.mas_equalTo(view).mas_offset(UIEdgeInsetsMake(10, 0, 10, 0)); ? 使用數(shù)組NSArray設(shè)置約束 make.height.equalTo(@[view1.mas_height, view2.mas_height]); make.height.equalTo(@[view1, view2]); make.left.equalTo(@[view1, @100, view3.right]); 使用優(yōu)先級(jí)設(shè)置約束.priorityHigh?<======>?UILayoutPriorityDefaultHigh ? ? 高優(yōu)先級(jí)
.priorityMedium?<========>?between high and low ? ? ???介于高/低之間
.priorityLow?<=========>?UILayoutPriorityDefaultLow ? 低優(yōu)先級(jí)
make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow(); make.top.equalTo(label.mas_top).with.priority(600); ? 使用MASCompositeConstraints添加約束 edges:邊緣 // make top, left, bottom, right equal view2 make.edges.equalTo(view2);// make top = superview.top + 5, left = superview.left + 10, // bottom = superview.bottom - 15, right = superview.right - 20 make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20)) // All edges but the top should equal those of the superview make.left.right.and.bottom.equalTo(superview); make.top.equalTo(otherView); ? size:大小 // make width and height greater than or equal to titleLabel make.size.greaterThanOrEqualTo(titleLabel)// make width = superview.width + 100, height = superview.height - 50 make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50)) ? center:中心 // make centerX and centerY = button1make.center.equalTo(button1)// make centerX = superview.centerX - 5, centerY = superview.centerY + 10 make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10)) 有時(shí)候,你需要修改現(xiàn)有的約束,以動(dòng)畫或刪除/替換約束。在砌體中有幾個(gè)不同的方法來(lái)更新約束。 1.使用設(shè)置References // in public/private interface @property (nonatomic, strong) MASConstraint *topConstraint; ... // when making constraints [view1 mas_makeConstraints:^(MASConstraintMaker *make) {self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);make.left.equalTo(superview.mas_left).with.offset(padding.left); }]; ... // then later you can call [self.topConstraint uninstall]; ? 2.更新約束?mas_updateConstraints - (void)updateConstraints {[self.growingButton mas_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]; } ? 3.重新設(shè)置mas_remakeConstraints - (void)changeButtonPosition { [self.button mas_remakeConstraints:^(MASConstraintMaker *make) {make.size.equalTo(self.buttonSize);if (topLeft) {make.top.and.left.offset(10);} else {make.bottom.and.right.offset(-10);}}]; }?具體的實(shí)例如下:設(shè)置view1舉例父視圖的四周距離均為50
?方式一: /** 方式一:使用NSLayoutConstraint實(shí)現(xiàn)手動(dòng)布局----------------------------設(shè)置UIEdgeInsets----------------------------@interface UIView (UIConstraintBasedLayoutInstallingConstraints)- (NSArray *)constraints NS_AVAILABLE_IOS(6_0);- (void)addConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0);- (void)addConstraints:(NSArray *)constraints NS_AVAILABLE_IOS(6_0);- (void)removeConstraint:(NSLayoutConstraint *)constraint- (void)removeConstraints:(NSArray *)constraints NS_AVAILABLE_IOS(6_0); */ -(void)LayoutConstraint {UIView *superView = self.view;UIView *view1 = [[UIView alloc]init];view1 .translatesAutoresizingMaskIntoConstraints = NO;view1.backgroundColor = [UIColor redColor];[superView addSubview:view1];//設(shè)置距離父視圖邊界距離UIEdgeInsets pading = UIEdgeInsetsMake(50, 50, 50, 50);//添加給view1約束 [superView addConstraints:@[[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1.0 constant:pading.top],[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:pading.left],[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-pading.bottom],[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-pading.right],]]; } ?方式二: /** 方法二:使用block @implementation MAS_VIEW (MASAdditions)----------------------------設(shè)置offset偏移 或者 邊緣edges ----------------------------- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block */ -(void)LayoutForMASConstraintMaker {UIView *superView = self.view;UIView *view1 = [[UIView alloc]init];view1.backgroundColor = [UIColor redColor];[superView addSubview:view1];//設(shè)置距離父視圖邊界距離UIEdgeInsets pading = UIEdgeInsetsMake(50, 50, 50, 50);//添加給view1約束[view1 mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(superView.mas_top).with.offset(pading.top);make.left.equalTo(superView.mas_left).with.offset(pading.left);make.bottom.equalTo(superView.mas_bottom).with.offset(-pading.bottom);make.right.equalTo(superView.mas_right).with.offset(-pading.right);//設(shè)置代碼可以更簡(jiǎn)單(效果與上面的是一樣的)//make.edges.equalTo(superView).with.insets(pading); }]; } ?方式三: /** 方式三:使用block @implementation MAS_VIEW (MASAdditions)----------------------------設(shè)置margin距離----------------------------- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block */ -(void)LayoutForMASConstraintMakerWithMargin {UIView *superView = self.view;UIView *view1 = [[UIView alloc]init];view1.backgroundColor = [UIColor redColor];[superView addSubview:view1];//添加給view1約束[view1 mas_makeConstraints:^(MASConstraintMaker *make) {make.topMargin.equalTo(superView.mas_top).with.offset(50);make.leftMargin.equalTo(superView.mas_left).with.offset(50);make.bottomMargin.equalTo(superView.mas_bottom).with.offset(-50);make.rightMargin.equalTo(superView.mas_right).with.offset(-50);}]; } ?方式四: /**方式四:使用block @implementation MAS_VIEW (MASAdditions)----------------------------設(shè)置center和size----------------------------- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block*/ -(void)LayoutForMASConstraintMakerWithCenterWidthHeight {UIView *superView = self.view;UIView *view1 = [[UIView alloc]init];view1.backgroundColor = [UIColor redColor];[superView addSubview:view1];//添加給view1約束[view1 mas_makeConstraints:^(MASConstraintMaker *make) {make.centerX.equalTo(superView);make.centerY.equalTo(superView);make.size.equalTo(superView).sizeOffset(CGSizeMake(-100,-100));}]; } ?演示結(jié)果: ?? ? ? ? 程序猿神奇的手,每時(shí)每刻,這雙手都在改變著世界的交互方式! 本文轉(zhuǎn)自當(dāng)天真遇到現(xiàn)實(shí)博客園博客,原文鏈接:http://www.cnblogs.com/XYQ-208910/p/5011483.html,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者總結(jié)
以上是生活随笔為你收集整理的iOS:Masonry练习详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 浅谈JavaScript继承
- 下一篇: 视频监控技术 迎来网络“多媒体”时代