从一开始的纯代码计算frame,虽然自认为计算frame 刚刚的,但是到后来还是开始xib的自动约束和手动约束与frame搭配使用,经历这几种方式,大概一年前开始普遍使用masonry来代码约束之后也跃跃欲试的自己体验了把,感觉还不错,分享下,比原生的好使多了。

使用步骤

1.添加Masonry文件夹的所有源代码到项目中(共两个Masonry这个文件夹,以及Masonry.framework)

2.添加两个宏定义导入头文件

// 只要添加了这个宏,就不用带mas_前缀

#define MAS_SHORTHAND

// 只要添加了这个宏,equalTo就等价于mas_equalTo

#define MAS_SHORTHAND_GLOBALS

// 这个头文件一定要放在上面两个宏的后面

#import "Masonry.h"

下面是添加约束的方法

// 这个方法只会添加新的约束

2  [view makeConstraints:^(MASConstraintMaker *make) {

3

4  }];

5

6 // 这个方法会将以前的所有约束删掉,添加新的约束

7  [view remakeConstraints:^(MASConstraintMaker *make) {

8

9  }];

10

11  // 这个方法将会覆盖以前的某些特定的约束

12  [view updateConstraints:^(MASConstraintMaker *make) {

13

14  }];

约束的类型

1.尺寸:width\height\size

2.边界:left\leading\right\trailing\top\bottom

3.中心点:center\centerX\centerY

4.边界:edges

*/

基本约束

UIView *superView = self.view;

//三个view布满整个屏幕

UIView *leftView = [[UIView alloc] init];

leftView.backgroundColor = [UIColor yellowColor];

[superView addSubview:leftView];

UIView *rightView = [[UIView alloc] init];

rightView.backgroundColor = [UIColor grayColor];

[superView addSubview:rightView];

UIView *bottomView = [[UIView alloc] init];

bottomView.backgroundColor = [UIColor greenColor];

[superView addSubview:bottomView];

int spacing = 0;

[leftView makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(self.view).offset(spacing);

make.top.equalTo(superView).offset(64);

make.right.equalTo(rightView.mas_leftMargin).offset(spacing);

make.bottom.equalTo(superView.mas_centerY).offset(32);

}];

[rightView makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(leftView.mas_rightMargin).offset(spacing);

make.right.equalTo(superView).offset(spacing);

make.top.equalTo(superView).offset(64);

make.width.equalTo(leftView);

make.height.equalTo(leftView);

}];

[bottomView makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(superView).offset(0);

make.right.equalTo(superView).offset(0);

make.bottom.equalTo(superView).offset(0);

make.top.equalTo(leftView.mas_bottom).offset(0);

make.height.equalTo(leftView);

}];

更换所有约束

- (void)viewDidLoad {

[super viewDidLoad];

yesOrNo = YES;

thisBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[thisBtn setTitle:@"click" forState:UIControlStateNormal];

[thisBtn addTarget:self action:@selector(updateMas) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:thisBtn];

[thisBtn makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(self.view).offset(10);

make.top.equalTo(self.view).offset(64 + 10);

make.width.equalTo(100);

make.height.equalTo(30);

}];

// Do any additional setup after loading the view.

}

-(void)updateMas

{

int num = arc4random()%300;

NSLog(@"%d",num);

[thisBtn remakeConstraints:^(MASConstraintMaker *make) {

make.bottom.equalTo(self.view).equalTo(- num - 20);

make.left.equalTo(self.view).equalTo(num);

make.width.equalTo(100);

make.height.equalTo(30);

}];

}

更新某个约束

- (void)viewDidLoad {

[super viewDidLoad];

btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btn setTitle:@"click" forState:UIControlStateNormal];

btn.backgroundColor = [UIColor grayColor];

[btn addTarget:self action:@selector(update) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

[btn makeConstraints:^(MASConstraintMaker *make) {

//make.centerX.equalTo(self.view.centerX);

//make.centerY.equalTo(self.view.centerY);

make.center.equalTo(self.view);

make.width.equalTo(100);

make.height.equalTo(30);

}];

// Do any additional setup after loading the view.

}

-(void)update

{

int width = arc4random()%300 +30;

[btn updateConstraints:^(MASConstraintMaker *make) {

make.width.equalTo(width);

}];

}

Scrollview

#import "ScrollviewViewController.h"

@interface ScrollviewViewController ()

@property(nonatomic,strong)UIScrollView *myScrollview;

@end

@implementation ScrollviewViewController

- (void)viewDidLoad {

[super viewDidLoad];

_myScrollview = [[UIScrollView alloc] init];

_myScrollview.backgroundColor = [UIColor grayColor];

[self.view addSubview:_myScrollview];

[self.myScrollview makeConstraints:^(MASConstraintMaker *make) {

make.edges.equalTo(self.view);

}];

[self makeScr];

// Do any additional setup after loading the view.

}

-(void)makeScr

{

UIView *contentView = UIView.new;

[self.myScrollview addSubview:contentView];

[contentView makeConstraints:^(MASConstraintMaker *make) {

make.edges.equalTo(self.myScrollview);

make.width.equalTo(self.myScrollview);

}];

UIView *lastView;

CGFloat height = 35 + arc4random()%100;

for (int i=0 ; i< 16; i++) {

UIView *view = UIView.new;

view.backgroundColor =[self randomColor];

[contentView addSubview:view];

[view makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(lastView ? lastView.bottom : @0);

make.left.equalTo(@0 );

make.width.equalTo(contentView.width);

make.height.equalTo(@(height));

}];

lastView = view;

}

[contentView makeConstraints:^(MASConstraintMaker *make) {

make.bottom.equalTo(lastView.bottom);

}];

}

- (UIColor *)randomColor {

CGFloat hue = ( arc4random() % 255 / 255.0 );  //  0.0 to 1.0

CGFloat saturation = ( arc4random() % 128 / 255.0 ) + 0.5;  //  0.5 to 1.0, away from white

CGFloat brightness = ( arc4random() % 128 / 255.0 ) + 0.5;  //  0.5 to 1.0, away from black

return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];

}

以上是一些最基本的简单使用,具体的还得自己加以研究啊。

需要注意的:

1 控件必需先添加在给约束。

以下是约束的代码demo

MasonryDemo.zip

iOS开发masonry的一些使用简介的更多相关文章

  1. iOS开发UI篇—核心动画简介

    转自:http://www.cnblogs.com/wendingding/p/3801036.html iOS开发UI篇—核心动画简介 一.简单介绍 Core Animation,中文翻译为核心动画 ...

  2. iOS开发-Masonry简易教程

    关于iOS布局自动iPhone6之后就是AutoLayOut,AutoLayOut固然非常好用,不过有时候我们需要在页面手动进行页面布局,VFL算是一种选择,如果对VFL不是很熟悉可以参考iOS开发- ...

  3. XMPPFrameWork IOS 开发(一)xmpp简介

    原始地址:XMPPFrameWork IOS 开发(一) XMPP : The Extensible Messaging and Presence Protocol 中文全称: 可扩展通讯和表示协议 ...

  4. iOS开发中KVC、KVO简介

    在iOS开发中,KVC和KVO是经常被用到的.可以使用KVC对对象的属性赋值和取得对象的属性值,可以使用KVO监听对象属性值的变化.简单介绍一下KVC和KVO. 一:键值编码(KVC) KVC,全称 ...

  5. iOS开发 Masonry的简单使用

    首先,在正式使用Masonry之前,我们先来看看在xib中我们是如何使用AutoLayout     从图中我们可以看出,只要设置相应得局限,控制好父视图与子视图之间的关系就应该很ok的拖出你需要的需 ...

  6. ios开发--高德地图SDK使用简介

    高德LBS开放平台将高德最专业的定位.地图.搜索.导航等能力,以API.SDK等形式向广大开发者免费开放.本章节我们来简单学习一下如何使用它的定位及地图SDK. 一.相关框架及环境配置 地图SDK 对 ...

  7. iOS开发 masonry 设置tableHeadView

    最近做公司项目,使用到到tableHeadView,一直习惯用masonry来设置约束,但是设置tableHeadView没有那么的简单.先看下效果图: 视图层次结构是这样的: 基础的创建工程项目之类 ...

  8. 李洪强漫谈iOS开发[C语言-007]-语言标准简介

    C语言是介于低级语言和高级语言之间的 一个应用程序 C语言在嵌入式上使用,的确是具有低级语言的特征 直接操作硬件,扫描内存 访问到的都是虚拟内存,一个应用程序占多大内存? 表示最多 可以放多少条指令 ...

  9. 【iOS开发-80】Quartz2D绘图简介:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState

    本文转载至 http://blog.csdn.net/weisubao/article/details/41282457 - (void)drawRect:(CGRect)rect { //获得当前上 ...

随机推荐

  1. sqlserver索引小结

    1.1 什么是索引? SQL索引有两种,聚集索引和非聚集索引,索引主要目的是提高了SQL Server系统的性能,加快数据的查询速度与减少系统的响应时间 下面举两个简单的例子: 图书馆的例子:一个图书 ...

  2. 【转载】PyQt QSetting保存设置

    转载地址: http://blog.sina.com.cn/s/blog_4b5039210100h3zb.html 用户对应用程序经常有这样的要求:要求它能记住它的settings,比如窗口大小,位 ...

  3. iOS NSOperation 封装 通知实现界面更新

    #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface MYOperation : NSOpe ...

  4. Structs框架

    一.准备工作及实例 1.解压struts-2.1.6-all.zip(structs网上下载) apps目录:struts2自带的例子程序 docs目录:官方文档. lib 目录:存放所有jar文件. ...

  5. 在wex5平台grid里面的gridselect下拉不能显示汉字问题

    当grid里面有gridSelect组件的时候,gridSelect里面的bind-ref是对应的数据库存入字段(int类型),bind-labelRef是对应的计算字段(视图里面的),而option ...

  6. C#中的Lambda表达式的演化过程

    原文:http://www.cnblogs.com/zhaopei/p/5767631.html

  7. android 修改 SwitchPreferenceCompat 高度,内边距,字体大小

    public class FontSizeSwitchPreferenceCompat extends SwitchPreferenceCompat { private Context mContex ...

  8. Jquery事件:鼠标移入移出(mouseenter,mouseleave)

    前几天帮朋友做了一个单页面,其中有个效果就是鼠标移动到头像上变换头像样式,当鼠标移出时恢复头像样式.当时没多想,脑子就蹦出了mouseover,mouseout两个方法. 但是在编写页面的过程中,无论 ...

  9. xcode8插件无法使用

    一,xcode8无法使用插件的问题 创建新的XcodeSigner代码证书,并执行"sudo codesign -f -s XcodeSigner /Applications/Xcode.a ...

  10. HTML—marquee

    滚动标签 支持的属性: 1.align 2.behavior: alternate: 表示在两端之间来回滚动.scroll: 表示由一端滚动到另一端,会重复.slide:  表示由一端滚动到另一端,不 ...