Masonry — 代码约束
来源:互联网 发布:淘宝vip 编辑:程序博客网 时间:2024/05/16 03:31
Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X。
Masonry是一个非常优秀的autolayout库 能够节省大量的开发和学习时间
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;
这些属性与NSLayoutAttrubute的对照表如下
下面是一个Masory工程的Demo
//// ViewController.m// Masnory_Demo//// Created by lanou3g on 15/10/29.// Copyright © 2015年 . All rights reserved.//#import "ViewController.h"#import "Masonry.h"#import "UIView+masnory.h"#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; WS(ws); //从此以后基本可以抛弃CGRectMake了 UIView *sv = [UIView new]; sv.backgroundColor = [UIColor blackColor]; //在做autoLayout之前 一定要先将view添加到superview上 否则会报错 [self.view addSubview:sv]; //mas_makeConstraints就是Masonry的autolayout添加函数 将所需的约束添加到block中行了 [sv mas_makeConstraints:^(MASConstraintMaker *make) { //将sv居中 make.center.equalTo(ws.view); //将size设置成(300,300) make.size.mas_equalTo(CGSizeMake(300, 300)); }];
#pragma mark -- [初级] 让一个view略小于其superView(边距为10)
// // UIView *sv1 = [UIView new];// sv1.backgroundColor = [UIColor redColor];// [sv addSubview:sv1];// //距离sv这个视图的边距// [sv1 mas_makeConstraints:^(MASConstraintMaker *make) {// make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));// // /* 等价于// make.top.equalTo(sv).with.offset(10);// make.left.equalTo(sv).with.offset(10);// make.bottom.equalTo(sv).with.offset(-10);// make.right.equalTo(sv).with.offset(-10);// */// // /* 也等价于// make.top.left.bottom.and.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));// */// }];
#pragma mark -- [初级] 让两个高度为150的view垂直居中且等宽且等间隔排列 间隔为10(自动计算其宽度)
// UIView *sv2 = [UIView new];// UIView *sv3 = [UIView new];// sv2.backgroundColor = [UIColor blueColor];// sv3.backgroundColor = [UIColor yellowColor];// // [sv1 addSubview:sv2];// [sv1 addSubview:sv3];// // int padding1 = 10;// [sv2 mas_makeConstraints:^(MASConstraintMaker *make) {// make.centerY.mas_equalTo(sv1.mas_centerY);// make.left.equalTo(sv1.mas_left).with.offset(padding1);// make.right.equalTo(sv3.mas_left).with.offset(-padding1);// make.height.mas_equalTo(@150);// make.width.equalTo(sv3);// }];// // [sv3 mas_makeConstraints:^(MASConstraintMaker *make) {// make.centerY.mas_equalTo(sv1.mas_centerY);// make.left.equalTo(sv2.mas_right).with.offset(padding1);// make.right.equalTo(sv1.mas_right).with.offset(-padding1);// make.height.mas_equalTo(@150);// // make.width.equalTo(sv2); 上面已经写过与其相等// }];#pragma mark -- [中级] 在UIScrollView顺序排列一些view并自动计算contentSize UIScrollView *scrollView = [UIScrollView new]; scrollView.backgroundColor = [UIColor whiteColor]; [sv addSubview:scrollView]; [scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(5,5,5,5)); }]; UIView *container = [UIView new]; [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); }];
#pragma mark -- [高级] 横向或者纵向等间隙的排列一组view
UIView *sv11 = [UIView new]; UIView *sv12 = [UIView new]; UIView *sv13 = [UIView new]; UIView *sv21 = [UIView new]; UIView *sv31 = [UIView new]; sv11.backgroundColor = [UIColor blackColor]; sv12.backgroundColor = [UIColor blackColor]; sv13.backgroundColor = [UIColor blackColor]; sv21.backgroundColor = [UIColor blackColor]; sv31.backgroundColor = [UIColor blackColor]; [sv addSubview:sv11]; [sv addSubview:sv12]; [sv addSubview:sv13]; [sv addSubview:sv21]; [sv addSubview:sv31]; //给予不同的大小 测试效果 [sv11 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(@[sv12,sv13]); make.centerX.equalTo(@[sv21,sv31]); make.size.mas_equalTo(CGSizeMake(40, 40)); }]; [sv12 mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(70, 20)); }]; [sv13 mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(50, 50)); }]; [sv21 mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(50, 20)); }]; [sv31 mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(40, 60)); }]; [sv distributeSpacingHorizontallyWith:@[sv11,sv12,sv13]]; [sv distributeSpacingVerticallyWith:@[sv11,sv21,sv31]];}@end
使用方法http://www.cocoachina.com/ios/20141219/10702.html
0 0
- Masonry — 代码约束
- iOS-使用代码约束布局(Masonry)
- Masonry约束
- 纯代码添加masonry约束注意点记录一
- 手写代码给ScrollView添加约束(Masonry)
- masonry 约束布局
- Masonry约束的使用
- masonry延时约束bug
- masonry约束的小细节
- 利用Masonry进行 约束判断
- 自动布局Masonry更新约束
- Masonry对UIScrollView添加约束
- masonry遇到Xib,约束异常
- Masonry学习之更新约束
- Masonry约束立即生效跟约束更新
- 如何使用Masonry第三方库,来约束纯代码中的控件
- 代码加约束Masonry的使用(快速上手Autolayout)及MMPlaceHolder标尺插件的使用
- iOS masonry动态约束控件位置
- 自定义 push 和 pop 实现有趣的相册翻开效果(上)
- 排序算法 之 选择排序
- Amaya呼吁更严格的DFS监管机制
- sqlite 获取数据库所有表名和字段名
- 无锁编程与有锁编程的性能对比与分析
- Masonry — 代码约束
- HTML:form表单总结,input,select,option,textarea,label
- SDK接入
- window下连接hadoop集群基础超详细版
- ios9 HTTP网络请求失败 and 第三方sdk 报错问题解决方法
- 使用java语言,利用多线程调用WebService进行数据处理
- Android 搭建系统服务
- 转一个 Xcode 7 缺少 *.dylib库的解决方法
- 简单的CXF实例