Masonry使用详解
mas_makeConstraints 只负责新增约束 Autolayout不能同时存在两条针对于同一对象的约束 否则会报错
mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况
mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束
equalTo 和 mas_equalTo的区别在哪里呢? 其实 mas_equalTo是一个MACRO,比较的是值,equalTo比较的是view。
宽高相等:
make.width.offset(marginHeight);
make.height.mas_equalTo(timeImg.mas_width).multipliedBy(1);
1. [基础] 居中显示一个view
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. WS(ws); UIView *sv = [UIView new]; [sv showPlaceHolder]; sv.backgroundColor = [UIColor blackColor]; [self.view addSubview:sv]; [sv mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(ws.view); make.size.mas_equalTo(CGSizeMake(300, 300)); }]; }UIView *sv1 = [UIView new];[sv1 showPlaceHolder];sv1.backgroundColor = [UIColor redColor];[sv addSubview:sv1];[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)); */}];int padding1 = 10;[sv2 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(sv.mas_centerY); make.left.equalTo(sv.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(sv.mas_centerY); make.left.equalTo(sv2.mas_right).with.offset(padding1); make.right.equalTo(sv.mas_right).with.offset(-padding1); make.height.mas_equalTo(@150); make.width.equalTo(sv2);}];UIScrollView *contextSV = [UIScrollView new];
contextSV.backgroundColor = [UIColor greenColor];
contextSV.pagingEnabled = YES;
[self.view addSubview:contextSV];
[contextSV mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(0);
make.right.offset(0);
make.top.offset(0);
make.bottom.offset(-49.0f);
}];
UIView *contentView = [UIView new];
[contextSV addSubview:contentView];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(0);
make.right.offset(0);
make.top.offset(0);
make.bottom.offset(0);
make.height.equalTo(contextSV.mas_height);
}];
UIView *nextView = nil;
for (int i = 0 ; i < 10; i++)
{
UIView *view = [UIView new];
view.backgroundColor = [UIColor colorWithRed:(arc4random()%256/255.0f) green:(arc4random()%256/255) blue:(arc4random()%256/255) alpha:1];
[contentView addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.offset(0);
make.bottom.offset(0);
if (i == 0) {
make.left.offset(0);
}else{
make.left.equalTo(nextView.mas_right).offset(0);
}
if (i == 9) {
make.right.offset(0);
}
make.width.equalTo(contextSV.mas_width);
}];
nextView = view;
}
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(nextView.mas_right);
}];
@implementation UIView(Masonry_LJC)- (void) distributeSpacingHorizontallyWith:(NSArray*)views{ NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1]; for ( int i = 0 ; i < views.count+1 ; ++i ) { UIView *v = [UIView new]; [spaces addObject:v]; [self addSubview:v]; [v mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(v.mas_height); }]; } UIView *v0 = spaces[0]; __weak __typeof(&*self)ws = self; [v0 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(ws.mas_left); make.centerY.equalTo(((UIView*)views[0]).mas_centerY); }]; UIView *lastSpace = v0; for ( int i = 0 ; i < views.count; ++i ) { UIView *obj = views[i]; UIView *space = spaces[i+1]; [obj mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(lastSpace.mas_right); }]; [space mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(obj.mas_right); make.centerY.equalTo(obj.mas_centerY); make.width.equalTo(v0); }]; lastSpace = space; } [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(ws.mas_right); }]; }- (void) distributeSpacingVerticallyWith:(NSArray*)views{ NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1]; for ( int i = 0 ; i < views.count+1 ; ++i ) { UIView *v = [UIView new]; [spaces addObject:v]; [self addSubview:v]; [v mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(v.mas_height); }]; } UIView *v0 = spaces[0]; __weak __typeof(&*self)ws = self; [v0 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(ws.mas_top); make.centerX.equalTo(((UIView*)views[0]).mas_centerX); }]; UIView *lastSpace = v0; for ( int i = 0 ; i < views.count; ++i ) { UIView *obj = views[i]; UIView *space = spaces[i+1]; [obj mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(lastSpace.mas_bottom); }]; [space mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(obj.mas_bottom); make.centerX.equalTo(obj.mas_centerX); make.height.equalTo(v0); }]; lastSpace = space; } [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(ws.mas_bottom); }];}@endUIView *sv11 = [UIView new];UIView *sv12 = [UIView new];UIView *sv13 = [UIView new];UIView *sv21 = [UIView new];UIView *sv31 = [UIView new];sv11.backgroundColor = [UIColor redColor];sv12.backgroundColor = [UIColor redColor];sv13.backgroundColor = [UIColor redColor];sv21.backgroundColor = [UIColor redColor];sv31.backgroundColor = [UIColor redColor];[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]];[sv showPlaceHolderWithAllSubviews];[sv hidePlaceHolder];_topScrollButtonTitleArr=[[NSMutableArray alloc] initWithObjects:@"推荐", @"青春", @"淑女", @"女鞋", @"配饰", @"女包", @"泳装", @"内衣", @"婚礼", @"大码", @"老公" ,@"妈妈", @"爸爸", @"孕妇", @"男孩", @"女孩", @"生活", nil];
__block MainChoseLabel *nextLabel=nil;
for (int i=0;i<_topScrollButtonTitleArr.count;i++) {
MainChoseLabel *mainLabel=[MainChoseLabel new];
[self.view addSubview:mainLabel];
mainLabel.backgroundColor=[UIColor redColor];
mainLabel.text=_topScrollButtonTitleArr[i];
int col=i%4;
[mainLabel mas_updateConstraints:^(MASConstraintMaker *make) {
if(nextLabel){
if(col==0){
make.left.offset(10);
}else{
make.left.equalTo(nextLabel.mas_right).offset(10);
}
make.width.equalTo(nextLabel);
if(((i+1)%4)==0){
make.right.offset(-10);
NSLog(@"---");
}
}else{
make.left.offset(10);
}
make.height.offset(44);
make.top.offset((10+44)*(i/4)+30);
}];
nextLabel=mainLabel;
}
}
_bigScrollView.pagingEnabled=YES;
_bigScrollView.delegate=self;
[self.view addSubview:_bigScrollView];
[_bigScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(120,5,5,5));
make.top.top.offset(0);
make.bottom.offset(0);
make.left.offset(0);
make.right.offset(0);
}];
_bigScrollView.backgroundColor=[UIColor redColor];
UIView *container = [UIView new];
[_bigScrollView addSubview:container];
container.backgroundColor=[UIColor redColor];
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_bigScrollView);
make.height.equalTo(_bigScrollView);
}];
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.top.and.bottom.equalTo(container);
make.width.mas_equalTo(_bigScrollView);
if (lastView)
{
make.left.mas_equalTo(lastView.mas_right);
}
else
{
make.left.mas_equalTo(container.mas_left);
}
}];
lastView = subv;
}
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(lastView.mas_right);
}];
Masonry使用详解的更多相关文章
- iOS:Masonry练习详解
Masonry练习详解 添加约束的方式: 1.通过使用NSLayoutConstraints添加约束到约束数组中,之前必须设置translatesAutoresizingMaskIntoConst ...
- Masonry练习详解
添加约束的方式: 1.通过使用NSLayoutConstraints添加约束到约束数组中,之前必须设置translatesAutoresizingMaskIntoConstraints = NO,即取 ...
- iOS开发——屏幕适配篇&Masonry详解
Masonry详解 前言 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-ip ...
- AutoLayout详解+手把手实战(转载)
首先说一下这篇博客虽然是标记为原创,但是事实并非本人亲自写出来的,知识点和例子本人花了一天各处查 找和整理最终决定写一个汇总的详解,解去各位朋友到处盲目查找的必要,因为不是转载某一个人的内容,故此不标 ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
随机推荐
- C#键盘事件处理
键盘事件是在用户按下键盘上的一个键的时候发生的,可分为两类.第一类是KeyPress事件,当按下的键表示的是一个ASCII字符的时候就会触发这类事件,可通过他的KeyPressEventArgs类型参 ...
- Centos6.3手动rpm安装gcc,c++
如果你的服务器是不能上网的,那就说明你要手动安装很多软件,比如gcc; 1,首先到http://vault.centos.org/6.3/os/x86_64/Packages/下载用到的rpm包,包括 ...
- hdu1013
#include<stdio.h> #include<string.h> int main() { char num[1000]; int len,sum,i; while(s ...
- 3、Web应用程序中的安全向量 -- cookie盗窃
作为用户,为了防止cookie盗窃,可以在浏览器设置中选择"禁用cookie",但是这样做很可能导致在访问某个站点的时候弹出警告"该站点必须使用cookie". ...
- deque (STL)
//双端队列 //deque的成员函数 c.assign(beg, end); //将[beg, end]区间中的数据赋值给c c.assign(n, elem); //将n个elem的拷贝赋值给c ...
- 怎样正确设置remote_addr和x_forwarded_for
怎样正确设置remote_addr和x_forwarded_for 2013-04-20 做网站时经常会用到remote_addr和x_forwarded_for这两个头信息来获取客户端的IP,然而当 ...
- WCF部署在IIS上
WCF部署在IIS上 环境vs2010,WCF应用程序.如何将WCF部署在IIS上. 第一步:右键点击项目,选择生成部署包. 第二步:在你项目所在的文件目录下找到Package文件夹,这就是我们的部署 ...
- postgresql 修改属性
up vote2down votefavorite From this article, I tried to update or delete property of a JSONB column: ...
- 快速破解ps方法
1.首先现在ps安装包和破解包. 2.运行Block Adobe Activation,防止ADOBE激活程序启动,按操作提示即可. 3.运行Adobe CS6安装程序. 4.选择“试用”. 5.输入 ...
- Strusts2--课程笔记4
类型转换器: Struts2默认情况下可以将表单中输入的文本数据转换为相应的基本数据类型.这个功能的实现,主要是由于Struts2内置了类型转换器.这些转换器在struts-default.xml中可 ...