iOS mas_makeConstraint约束和autolayout
来源:互联网 发布:002558 巨人网络 编辑:程序博客网 时间:2024/06/05 21:01
要创建一个试图,距离上下左右都是10的这样一个约束需要写上很多代码,然而现在是使用Masonry的效果
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);[view1 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler make.left.equalTo(superview.mas_left).with.offset(padding.left); make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom); make.right.equalTo(superview.mas_right).with.offset(-padding.right);}];
甚至我们这样写得更加简洁
[view1 mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(superview).with.insets(padding);}];
接下来我们来观看下Masonry中的一些常用属性
// 左侧 @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; // 中心点x @property (nonatomic, strong, readonly) MASConstraint *centerX; // 中心点y @property (nonatomic, strong, readonly) MASConstraint *centerY; // 文本基线 @property (nonatomic, strong, readonly) MASConstraint *baseline;
居中显示视图
UIView *myView = [[UIView alloc] init];myView.backgroundColor = [UIColor blueColor];[self.view addSubview:myView]; [myView mas_makeConstraints:^(MASConstraintMaker *make) { // 设置当前center和父视图的center一样 make.center.mas_equalTo(self.view); // 设置当前视图的大小 make.size.mas_equalTo(CGSizeMake(300, 300));}];
效果图
可以看到我们已经创建出一个位置居中,并且视图大小为300×300
设置视图并排
UIView *view1 = [[UIView alloc] init];view1.backgroundColor = [UIColor redColor];[myView addSubview:view1]; UIView *view2 = [[UIView alloc] init];view2.backgroundColor = [UIColor yellowColor];[myView addSubview:view2]; int padding = 10; [view1 mas_makeConstraints:^(MASConstraintMaker *make) { // 设置其位于父视图的Y的中心位置 make.centerY.mas_equalTo(myView.mas_centerY); // 设置其左侧和父视图偏移10个像素 make.left.equalTo(myView).with.offset(padding); // 设置其右侧和view2偏移10个像素 make.right.equalTo(view2.mas_left).with.offset(-padding); // 设置高度 make.height.mas_equalTo(@120); // 设置其宽度 make.width.equalTo(view2);}]; [view2 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(myView.mas_centerY); make.left.equalTo(view1.mas_right).with.offset(padding); make.right.equalTo(myView).with.offset(-padding); make.height.mas_equalTo(view1); make.width.equalTo(view1);}];
效果图:
提醒一下,以下代码等价
make.left.equalTo(myView).with.offset(padding); 】 // 等价于 make.left.equalTo(myView.mas_left).with.offset(padding);
也就是说默认情况下括号里面只写了视图的时候,其自动帮你添加当前masxxx(代表前面你需要设置的约束的位置).比如上面两行代码设置的make.left,当括号里面只写了myView的时候,会自动追加为myView.mas_left。
多个视图间隔相同
注意下面设置宽度的时候是传递的数组,这样才能让多个视图进行等距离显示
UIView *view1 = [[UIView alloc] init];view1.backgroundColor = [UIColor redColor];[myView addSubview:view1]; UIView *view2 = [[UIView alloc] init];view2.backgroundColor = [UIColor yellowColor];[myView addSubview:view2]; UIView *view3 = [[UIView alloc] init];view3.backgroundColor = [UIColor greenColor];[self.view addSubview:view3]; int padding = 10;[view1 mas_makeConstraints:^(MASConstraintMaker *make) { // 设置中心点 make.centerY.mas_equalTo(myView); // 设置左侧距离父视图10 make.left.equalTo(myView).with.offset(padding); // 设置右侧距离和view2的左侧相隔10 make.right.equalTo(view2.mas_left).with.offset(-padding); // 设置高度 make.height.mas_equalTo(@150); // 宽度设置和view2以及view3相同 make.width.equalTo(@[view2, view3]);}]; [view2 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(myView); make.height.mas_equalTo(view1); make.width.equalTo(@[view1, view3]);}];[view3 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(myView); make.left.equalTo(view2.mas_right).with.offset(padding); make.right.equalTo(myView).with.offset(-padding); make.height.mas_equalTo(view1); make.width.equalTo(@[view2, view1]);}];
效果图:
0 0
- iOS mas_makeConstraint约束和autolayout
- mas_makeConstraint约束和autolayout
- iOS Autolayout约束动画化
- iOS autolayout检测是否欠约束
- iOS 代码添加约束—VFL (AutoLayout)
- iOS 代码添加约束—VFL (AutoLayout)
- ios Autolayout改变约束, 动画效果处理
- iOS开发:XIB之AutoLayout添加约束
- [IOS 开发] Autolayout约束动画化
- ios-autolayout添加约束的规则
- AutoLayout约束
- iOS autolayout和VFL
- iOS autolayout和sizeclass
- 关于AutoLayout和代码修改约束
- IOS开发-14.利用代码添加autolayout约束
- iOS UI设计: autolayout约束的一点技巧
- iOS在纯代码中使用AutoLayout添加约束
- iOS AutoLayout自动布局修改约束的值,延迟加载
- [leetcode 435. Non-overlapping Intervals] medium|week 8
- pyZooKeeperHelper.py实现Zookeeper的自动化配置
- swustoj回文数(0371)
- 比较时间大小
- SQLServer 实现oracle中rownum 的功能
- iOS mas_makeConstraint约束和autolayout
- mysql limit分页 SQL语句模糊查询
- 第一次使用Android Studio开发心得
- protobuf 语法和注意事项
- Activity堆栈管理
- 安装Redhat 7.2
- 书写高质量jQuery代码的12条经验
- jsp和struct2的路径问题
- MYSQL中常用的强制性操作(例如强制索引)