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功能惊叹的部分,所以趁热打铁写 ...
随机推荐
- C 构造一个 简单配置文件读取库
前言 最近看到这篇文章, json引擎性能对比报告 http://www.oschina.net/news/61942/cpp-json-compare?utm_source=tuicool 感觉技术 ...
- struct的成员对齐问题-结构体实际大小问题
struct的成员对齐 注意:为了方便说明,等号左边是每个数据单独所占长度,右边是最终空间大小,以字节为单位. 一.什么时间存在对其问题:(32位机对齐方式是按照4字节对其的,以下所有试验都是在32位 ...
- oracle11g 数据文件误删恢复(无备份)
OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - ...
- gravity、layout_gravity、ayout_weight 区别及用法
layout_gravity 表示组件自身在父组件中的位置. gravity 表示组件的子组件在组件中的位置. weight 意思是权重.比重,即当前控件在画布中所占的空间 ...
- bzoj 3196/tyvj p1730 二逼平衡树
原题链接:http://www.tyvj.cn/p/1730 树套树... 如下: #include<cstdio> #include<cstdlib> #include< ...
- eclipse创建android项目失败的问题 [ android support library ]
有根筋搭错了,想起来android应用开发???? 放下两年的手机应用开发,昨天有更新了android SDK, 重新搭建开发环境. 这两年android 变化真TM的大............... ...
- How to compile and install NCAR Command Language on IBM PowerPC 64 --- NCL编译安装步骤
作者:Sinsonglew 出处:http://www.cnblogs.com/sinsonglew 欢迎转载,也请保留这段声明.thanks :) 注记:NCL官方依赖安装包全集列表.官方源码编译指 ...
- eclipse格式化代码末班修改
在窗口->首选项->输入format(格式)搜索,或者找Java->代码样式->格式化程序: 几个内置的不能调格式化代码风格,但是可以根据内置的新建一个,出来很多选项,开始调吧 ...
- Teamwork——Week4 团队项目之NABC
项目框架——NABC模型 一.N(Need需求) 我们组主要的用户对象是第三小组——UI小组的同学们,因此我们的用户需求就是他们的数据需求. 1)提供给UI小组整理好的数据库,和前一组讨论好数据结构. ...
- Team Homework #2 Decide the roles of each team member ——IloveSE
大家好,我们是IloveSEers! 徐姗,我是一个性格开朗,但却认为计算机比较枯燥的女生.经过两年的学习,自己的编程能力,并不是很强,在这方便还需多多练习.对于软件工程这门课,我充满期待,因为我不仅 ...