博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Masonry适配
阅读量:6510 次
发布时间:2019-06-24

本文共 3454 字,大约阅读时间需要 11 分钟。

hot3.png

////  ViewController.m//  Masonry适配////  Created by asun on 16/5/24.//  Copyright © 2016年 wuchang. All rights reserved.//#import "ViewController.h"#import 
@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; CGFloat width = WIDTH / 5.0; UIView *view = [[UIView alloc]init]; view.backgroundColor = [UIColor yellowColor]; [self.view addSubview:view]; [view mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self.view);//屏幕居中 make.width.and.height.mas_equalTo(width); }]; UILabel *label1 = [[UILabel alloc]init]; label1.backgroundColor = [UIColor redColor]; [self.view addSubview:label1]; [label1 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(view); //X轴在控件view中居中显示 make.top.equalTo(view).offset(JWScaleY(10)); //距离顶部 make.bottom.equalTo(view).offset(JWScaleY(-30));//距离底部 (底部和右侧都是负数) make.width.equalTo(view).offset(view.width - JWScaleY(40));//宽度 }]; UILabel *label2 = [[UILabel alloc]init]; label2.textAlignment = NSTextAlignmentCenter; label2.text = @"label2"; [self.view addSubview:label2]; [label2 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(label1.mas_bottom).offset(0); //距离lab1底部0像素 make.width.mas_equalTo(width); make.centerX.equalTo(view); //X轴在控件view中居中显示 make.bottom.equalTo(view); //label2底部和view的底部重叠 }]; }- (void) layout{ UIView *views = [[UIView alloc]init]; views.backgroundColor = [UIColor blackColor]; [self.view addSubview:views]; //在做适配之前需要先把控件加到视图上,要不然会报错 __weak typeof(self)weakSelf = self; [views mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(weakSelf.view); make.size.mas_equalTo(CGSizeMake(300, 300)); }]; UIView *view1 = [[UIView alloc]init]; view1.backgroundColor = [UIColor yellowColor]; [views addSubview:view1]; [view1 mas_makeConstraints:^(MASConstraintMaker *make) { // make.edges.equalTo(view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10)); //或者 // make.top.equalTo(view).offset(10); // make.left.equalTo(view).offset(10); // make.right.equalTo(view).offset(-10); // make.bottom.equalTo(view).offset(-10); //在或者 make.top.left.right.bottom.equalTo(weakSelf.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10)); }]; UIView *view2 =[[UIView alloc]init]; view2.backgroundColor = [UIColor redColor]; [view1 addSubview:view2]; UIView *view3 = [[UIView alloc]init]; view3.backgroundColor = [UIColor blueColor]; [view1 addSubview:view3]; [view2 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(view1).offset(10); make.left.equalTo(view1).offset(10); make.bottom.equalTo(view1).offset(-10); make.width.mas_equalTo(100); }]; [view3 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(view2.mas_right).offset(10);//设置view3的左侧距离view2的右侧10 make.top.equalTo(view1).offset(10); make.right.equalTo(view1).offset(-10); make.bottom.equalTo(view1).offset(-10); }];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end

 

转载于:https://my.oschina.net/u/2287505/blog/1634428

你可能感兴趣的文章
554 SBRS score too low : http://www.senderbase.org
查看>>
iphone开发之viewDidLoad
查看>>
返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性
查看>>
用命令启动控制面板
查看>>
JAVA - 优雅的记录日志(log4j实战篇)
查看>>
Android编码规范
查看>>
Win 7的密码破解及更改登录默认用户名
查看>>
android中文api (59) —— TabHost.TabSpec
查看>>
常用网线制作 连接方式
查看>>
Android 中文API (67) —— BluetoothClass.Device.Major
查看>>
蓝牙通信(一)——打开和关闭蓝牙
查看>>
Maven 参数(maven.test.skip 和 maven.test.skip.exec) 区别
查看>>
Linux shell输出指定的行
查看>>
学习C++的50条忠告
查看>>
Oracle中字符缓冲区相关错误
查看>>
模拟“博客搬家”脚本-第一天
查看>>
关于Unity中的资源管理,你可能遇到这些问题
查看>>
Hyper-v 3.0 安装centos6.3
查看>>
详解SCVMM 2008中的虚拟机模板
查看>>
moosefs 测试
查看>>