Masonry自动布局
介绍,入门:
http://www.cocoachina.com/ios/20141219/10702.html
下载:
1.Masonry初体验:
//
// ViewController.m
// MasonryTest
//
// Created by apple on 15/6/22.
// Copyright (c) 2015年 tqh. All rights reserved.
// #import "ViewController.h"
#import "Masonry.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// 防止block中的循环引用
[self initView4];
} - (void)initView1 {
UIButton *button = [[UIButton alloc]init];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
[self Masonry:button];
} //居中约束
- (void)Masonry:(UIView *)view {
__weak typeof (self) weakSelf = self;
[view mas_makeConstraints:^(MASConstraintMaker *make) {
//大小约束
make.size.mas_equalTo(CGSizeMake(, ));
//居中约束
make.center.equalTo(weakSelf.view);
}];
} //固定大小,位置调整 - (void)initView2 {
UIButton * blackBtn = [UIButton new];
blackBtn.backgroundColor = [UIColor blackColor];
[self.view addSubview:blackBtn];
[blackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo();
make.left.mas_equalTo();
make.size.mas_equalTo(CGSizeMake(, ));
}];
UIButton * redBtn = [UIButton new];
redBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:redBtn];
[redBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//使用 and 连接
make.size.and.top.equalTo(blackBtn);
//添加右边距约束(这里的间距是有方向性的,左、上边距约束正数,右、下边距约束为负数)
make.right.mas_equalTo(-);
}];
} - (void)initView3 {
// 防止block中的循环引用
__weak typeof (self) weakSelf = self;
UIButton * redBtn = [[UIButton alloc]init];
redBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:redBtn];
[redBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.left.mas_equalTo();
make.right.mas_equalTo(-);
// make.bottom.and.right.mas_equalTo(-20);
}];
UIButton * blueBtn = [UIButton new];
blueBtn.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueBtn];
[blueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.and.right.mas_equalTo(-);
//让他等于redBtn的高度
make.height.equalTo(redBtn);
//添加上左约束
make.top.equalTo(redBtn.mas_bottom).offset();
make.left.equalTo(weakSelf.view.mas_centerX).offset(-);
}];
} - (void)initView4 {
// 左边的按键
UIButton * firstBtn = [[UIButton alloc]init];
firstBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:firstBtn];
// 右边的按键
UIButton * secondBtn = [[UIButton alloc]init];
secondBtn.backgroundColor = [UIColor blueColor];
[self.view addSubview:secondBtn];
int padding1 = ;
// 给左边视图添加约束
[firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo();
make.left.mas_equalTo();
make.right.equalTo(secondBtn.mas_left).with.offset(-padding1);
}];
// 给右边视图添加约束
[secondBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo();
make.right.mas_equalTo(-);
make.width.equalTo(firstBtn);
}];
} - (void)initView4Test {
for (int i = ; i < ; i ++) { UIView * firstBtn = [[UIView alloc]init];
firstBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:firstBtn];
[firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo();
make.size.mas_equalTo(CGSizeMake(, ));
make.left.mas_equalTo(i*);
}];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPressed:)];
firstBtn.tag = +i;
firstBtn.userInteractionEnabled = YES;
[firstBtn addGestureRecognizer:tap];
}
} - (void)tapPressed:(UITapGestureRecognizer *)sender {
NSInteger index = sender.view.tag;
NSLog(@"%ld",index);
} @end
有待进一步熟悉
补:
自动布局动态计算cell的高度
http://www.ifun.cc/blog/2014/02/21/dong-tai-ji-suan-uitableviewcellgao-du-xiang-jie/
http://blog.sunnyxx.com/2015/05/17/cell-height-calculation/
Masonry自动布局的更多相关文章
- Masonry自动布局使用
		
Masonry是一个轻量级的布局框架,采用更好的语法封装自动布局,它有自己的布局DSL.简洁明了并具有高可读性 而且同时支持 iOS 和 Max OS X. 下载 NSLayoutConstraint ...
 - Masonry自动布局与UIScrolView适配
		
Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X.可以通过cocoa ...
 - Masonry自动布局:复合约束
		
前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...
 - IOS Masonry自动布局
		
之前项目用Frame布局,这个项目登录用了VFL,后来觉得用Masonry,前天布局TableViewCell时用了下 ,觉得还不错. #import "Masonry.h" #i ...
 - 【iOS】Masonry 自动布局 MASViewConstraint.m:207 错误
		
问题详情: Assertion failure 报错原因: make.right.equalTo([_imageView superview]).right.with.offset(-); make. ...
 - Coding源码学习第四部分(Masonry介绍与使用(三))
		
接上篇继续进行Masonry 的学习. (12)tableViewCell 布局 #import "TableViewController.h" #import "Tes ...
 - masonry使用问题
		
2015年11月3日 coreData的学习练习中复习使用masonry自动布局 masonry自动布局发现问题: 两个控件的相对布局: 如果被参考对象用这个带anchor的属性,就会报这样一个错误: ...
 - iOS masonry 不规则tagView布局 并自适应高度
		
在搜索页面经常会有不规则的tag出现,这种tagView要有点击事件,单个tagView可以设置文字颜色,宽度不固定根据内容自适应,高度固定,数量不固定.总高度就不固定.最近对于masonry的使用又 ...
 - Masonry学习札记
		
Masnory学习札记 在之前的文章里有草草提到过Masonry自动布局,可这么重要第三方布局框架的怎么可以怎么随便带过呢!昨天在完成页面的时候刚好遇到了被Masorny功能惊叹的部分,所以趁热打铁写 ...
 
随机推荐
- 窗体皮肤实现 - 实现简单Toolbar(六)
			
自定义皮肤很方便,基础开发的工作也是很大的.不过还好一般产品真正需要开发的并不是很多.现在比较漂亮的界面产品都会有个大大的工具条. Toolbar工具条实现皮肤的方法还是可以使用Form的处理方案.每 ...
 - windows下查看所有进程以及pid
			
import ctypes import sys __metaclass__ = type class PROCESSENTRY32(ctypes.Structure): _fields_ = [ ( ...
 - iOS学习之UITabBarController
			
一.标签视图控制器——UITabBarController 1.UITabBarController的继承关系: @interface UITabBarController : UIViewContr ...
 - centos 安装phantomjs
			
sudo yum install gcc gcc-c++ make git openssl-devel freetype-devel fontconfig-devel git clone git:// ...
 - [quote ]ffmpeg, gstreamer, Raspberry Pi, Windows Desktop streaming
			
[quote ]ffmpeg, gstreamer, Raspberry Pi, Windows Desktop streaming http://blog.pi3g.com/2013/08/ffmp ...
 - win7无线网卡的灯突然不亮了的解决办法
			
win7无线网卡的灯突然不亮了,百度了一下,按如下的方法解决了: WIN7中:右键单击“计算机”,选择“管理”进入“计算机管理”,选择“服务和运用”下的“服务”,然后双击“WLAN AutoConf ...
 - JAVA类与对象(四)----成员变量与局部变量 、成员方法、构造方法
			
类体中的变量分为两部分.变量定义部分定义的变量为类的成员变量,在方法体中定义的变量和方法中涉及的变量称为局部变量. 成员变量和局部变量的区别: (1).成员变量在整个类中都有效,局部变量只在定义它的方 ...
 - STL学习系列四:Stack容器
			
Stack简介 stack是堆栈容器,是一种“先进后出”的容器. stack是简单地装饰deque容器而成为另外的一种容器. #include <stack> 1.stack对象的默认构造 ...
 - cocos2dx中的CCLayerColor
			
颜色图层在游戏中主要用来烘托背景,可以按照RGB设置填充颜色,同时还可以设置图层的透明度(opacity),常用于显示背景 颜色图层还存在一个特殊的子类:CCLayerGradient,是具有颜色渐变 ...
 - 设置浮点数的显示精度&precision(0)
			
/* 设置浮点数的显示精度 cout.precision(int)可以设置浮点数的显示精度(不包括小数点) 注: 1.如果设置的精度大于浮点数的位数,如果浮点数能根据IEEE ...