Masonry练习详解

添加约束的方式:

1.通过使用NSLayoutConstraints添加约束到约束数组中,之前必须设置translatesAutoresizingMaskIntoConstraints = NO,即取消自动布局;
 
2.通过使用MASConstraintMaker在block中添加约束,不需要再设置translatesAutoresizingMaskIntoConstraintst 属性,block内部已经帮助完成;

约束的关系:

equalTo  <=======>   NSLayoutRelationEqual   等于

lessThanOrEqualTo   <======>  NSLayoutRelationLessThanOrEqual   小于或等于

greaterThanOrEqualTo <=======>  NSLayoutRelationGreaterThanOrEqual  大于或等于

 

MASViewAttribute:视图约束属性

UIView/NSView
这两个约束完全相同,都是view左边大于等于label的左边位置
make.left.greaterThanOrEqualTo(label);
make.left.greaterThanOrEqualTo(label.mas_left);
NSNumber给约束设置具体的值
<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,使用原始的数据或者结构体设置约束数据
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));
 
使用数组NSArray设置约束
make.height.equalTo(@[view1.mas_height, view2.mas_height]);
make.height.equalTo(@[view1, view2]);
make.left.equalTo(@[view1, @100, view3.right]);

使用优先级设置约束

.priorityHigh <======> UILayoutPriorityDefaultHigh     高优先级

.priorityMedium <========> between high and low        介于高/低之间

.priorityLow <=========> UILayoutPriorityDefaultLow   低优先级

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))

有时候,你需要修改现有的约束,以动画或删除/替换约束。在砌体中有几个不同的方法来更新约束。
1.使用设置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.重新设置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);}}];
}

具体的实例如下:设置view1举例父视图的四周距离均为50

 方式一:
/**
方式一:使用NSLayoutConstraint实现手动布局----------------------------设置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];//设置距离父视图边界距离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)----------------------------设置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];//设置距离父视图边界距离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);//设置代码可以更简单(效果与上面的是一样的)//make.edges.equalTo(superView).with.insets(pading);
    }];
}

 方式三:
/**
方式三:使用block  @implementation MAS_VIEW (MASAdditions)----------------------------设置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)----------------------------设置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));}];
}

 演示结果:
  
 
 
 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5011483.html,如需转载请自行联系原作者

iOS:Masonry练习详解相关推荐

  1. Masonry自动布局详解一:基本用法

    Masonry自动布局详解一:基本用法 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更 ...

  2. Masonry自动布局详解五:比例(multipliedBy)

    Masonry自动布局详解五:比例(multipliedBy) 标签: iosmasonryautolayout自动布局约束 2015-11-30 16:30 1816人阅读 评论(0) 收藏 举报 ...

  3. iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)

    前言:一个路径可以包含由一个或者多个shape以及子路径subpath,quartz提供了很多方便的shape可以直接调用.例如:point,line,Arc(圆弧),Curves(曲线),Ellip ...

  4. IOS 多线程04-GCD详解 底层并发 API

    IOS 多线程04-GCD详解 底层并发 API 注:本人是翻译过来,并且加上本人的一点见解. 前言 想要揭示出表面之下深层次的一些可利用的方面.这些底层的 API 提供了大量的灵活性,随之而来的是大 ...

  5. iOS核心动画详解swift版----基础动画

    2019独角兽企业重金招聘Python工程师标准>>> iOS核心动画详解swift版---基础动画 创建工程,添加2个ViewController,通过rootViewContro ...

  6. iOS教程:详解iOS多图下载的缓存机制

    ios教程,ios的干货一直来不及给大家分享,小编也是一直在忙啊!今天给大家献上ios:详解iOS多图下载的缓存机制 1. 需求点是什么? 这里所说的多图下载,就是要在tableview的每一个cel ...

  7. iOS TableView 使用详解

     IOS TableView 详解 一.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ...

  8. iOS 内购详解-代码篇

    内购项目-代码篇 一.分步骤说明 1.获取商品列表 2.苹果服务器返回的可购买商品 3.下单购买商品 4.购买队列状态变化,判断购买状态是否成功 5.交易验证 6.拿到的收据信息是,此App所有购买的 ...

  9. ios NSFileManager 用法详解

    2019独角兽企业重金招聘Python工程师标准>>> iPhone文件系统NSFileManager讲解是本文要介绍的内容,主要是通过iphone文件系统来学习NSFileMana ...

最新文章

  1. 输入对话框基于PyQt4的输入对话框
  2. Java语法基础-1
  3. Linux之DNS服务器搭建及常见DNS***和防御
  4. 初学者linux和ubuntu,linux初学者也必须知道的几个ubuntu最基础命令
  5. post 表单中常见的四种表单请求方式
  6. Java命令行界面(第9部分):parse-cmd
  7. Vue之实例的生命周期
  8. 电子政务项目风险管理(上)
  9. python使用函数目的_Python函数的概念和使用
  10. JavaScript类型强制解释
  11. 视频编码技术---压缩感知编码---匹配跟踪算法
  12. java监听mysql某张表的insert操作_MySql轻松入门系列————第一站 从源码角度轻松认识mysql整体框架图
  13. 面试—每日一题(8)
  14. 陌陌 3 千万数据暗网出售;美团反腐 89 人受刑事查处;iPhone 推迟 5G 采用时间 | 极客头条...
  15. JavaScript高级程序设计笔记 事件冒泡和事件捕获
  16. Mac访达搜索找不到对应文件的解决方法
  17. JanusGraph
  18. run `npm fund` for details
  19. 如何正确地在Spring Data JPA和Jackson中用上Java 8的时间相关API(即JSR 310也即java.time包下的众神器)...
  20. OTA制作及升级过程笔记

热门文章

  1. vue html5模板,vue-h5-template
  2. java webservice 常用_复杂对象类型的WebService高级部分
  3. 被替换的项目不是替换值长度的倍数_面试官,为啥HashMap的长度是2的n次方?
  4. python有哪些方面_Python学习中最基本的内容,看看有哪些需要我们学习的
  5. r k-means 分类结果_机器学习-Kmeans均值聚类算法(贪心学院)
  6. php-ast 开源,PHP AST学习 - osc_ryjlu6z2的个人空间 - OSCHINA - 中文开源技术交流社区...
  7. golang mac 环境变量_macOS 配置 golang 运行环境
  8. c# u盘使用记录_C# 系统应用之通过注册表获取USB使用记录(一)
  9. 内地计算机科技学校,23所内地顶尖高校盘点!计算机学科哪家强?
  10. Netty原理架构解析