博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Masonry代码自动布局的简单使用。
阅读量:5166 次
发布时间:2019-06-13

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

Masonry是用代码实现自动布局的第三方框架。

使用之前首先要导入框架,以下是具体的代码实现。

1.中心点与父视图相同

 

#import "ViewController.h"#import "Masonry.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];

//    [self setView1];

 

//    [self setView2];

 

    //[self setView3];

[self setView4];}/** *  中心点与父视图相同 */-(void)setView1{    UIView *mainView = [[UIView alloc] init];    mainView.backgroundColor = [UIColor redColor];    [self.view addSubview:mainView];    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {        make.center.equalTo(self.view);        make.size.mas_equalTo(CGSizeMake(200, 200));    }];}

 

2.距离上下左右边距

 

/** *  距离上下左右边距 */-(void)setView2{    UIView *mainView = [[UIView alloc] init];    mainView.backgroundColor = [UIColor redColor];    [self.view addSubview:mainView];    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {        //make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(100, 80, 50, 10));        make.left.equalTo(self.view).with.offset(10);        make.right.equalTo(self.view).with.offset(-20);        make.top.equalTo(self.view).with.offset(30);        make.bottom.equalTo(self.view).with.offset(-40);    }];

 

 3.两个视图左右排开间距是10

 

-(void)setView3{    //左边视图    UIView *rightView = [[UIView alloc] init];    rightView.backgroundColor = [UIColor redColor];    [self.view addSubview:rightView];    //右边视图    UIView *leftView1 = [[UIView alloc] init];    leftView1.backgroundColor = [UIColor greenColor];    [self.view addSubview:leftView1];     [rightView mas_makeConstraints:^(MASConstraintMaker *make) {     make.centerY.mas_equalTo(self.view.mas_centerY);     make.height.mas_equalTo(150);     make.width.mas_equalTo(leftView1.mas_width);     make.left.mas_equalTo(self.view.mas_left).with.offset(10);     make.right.mas_equalTo(leftView1.mas_left).with.offset(-10); }]; [leftView1 mas_makeConstraints:^(MASConstraintMaker *make) {        make.centerY.mas_equalTo(self.view.mas_centerY);        make.height.mas_equalTo(150);        make.width.mas_equalTo(rightView.mas_width);        make.left.mas_equalTo(rightView.mas_right).with.offset(10);        make.right.mas_equalTo(self.view.mas_right).with.offset(-10);    }];}

 

4.登录界面

-(void)setView4{    UITextField *accountTextField = [[UITextField alloc] init];    accountTextField.backgroundColor = [UIColor redColor];    accountTextField.placeholder = @"账号";    [self.view addSubview:accountTextField];        UITextField *secretTextField = [[UITextField alloc] init];    secretTextField.backgroundColor = [UIColor greenColor];    secretTextField.placeholder = @"密码";    [self.view addSubview:secretTextField];    UIButton *loginButton = [[UIButton alloc] init];    loginButton.backgroundColor = [UIColor blueColor];    [loginButton setTitle:@"登录" forState:UIControlStateNormal];    [self.view addSubview:loginButton];    [accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {        make.left.equalTo(self.view.mas_left).offset(50);        make.right.equalTo(self.view.mas_right).offset(-50);        make.top.equalTo(self.view.mas_top).offset(100);        make.height.mas_equalTo(50);    }];    [secretTextField mas_makeConstraints:^(MASConstraintMaker *make) {        make.left.equalTo(self.view.mas_left).offset(50);        make.right.equalTo(self.view.mas_right).offset(-50);        make.top.equalTo(accountTextField.mas_bottom).offset(40);        make.height.mas_equalTo(50);    }];    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {        make.left.equalTo(self.view.mas_left).offset(100);        make.right.equalTo(self.view.mas_right).offset(-100);        make.top.equalTo(secretTextField.mas_bottom).offset(40);        make.height.mas_equalTo(50);    }];    }

以下为源码连接地址:http://pan.baidu.com/s/1eRnfs8i

 

转载于:https://www.cnblogs.com/DLS520/p/5100001.html

你可能感兴趣的文章
iOS之文本属性Attributes的使用
查看>>
从.Net版本演变看String和StringBuilder性能之争
查看>>
Excel操作 Microsoft.Office.Interop.Excel.dll的使用
查看>>
解决Ubuntu下博通网卡驱动问题
查看>>
【bzoj2788】Festival
查看>>
执行gem install dryrun错误
查看>>
HTML5简单入门系列(四)
查看>>
实现字符串反转
查看>>
转载:《TypeScript 中文入门教程》 5、命名空间和模块
查看>>
苹果开发中常用英语单词
查看>>
[USACO 1.4.3]等差数列
查看>>
Shader Overview
查看>>
Reveal 配置与使用
查看>>
Java中反射的学习与理解(一)
查看>>
C语言初学 俩数相除问题
查看>>
B/S和C/S架构的区别
查看>>
[Java] Java record
查看>>
jQuery - 控制元素显示、隐藏、切换、滑动的方法
查看>>
postgresql学习文档
查看>>
Struts2返回JSON数据的具体应用范例
查看>>