Autolayout讲解较多的就是xib和storyboard用法,本文主要记录纯代码的Autolayout使用方法:

方法1.苹果原生的方法,这种方法虽然简单但是太过繁杂,可用性很差

    //宽度=superView高度
[superView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeWidth multiplier: constant:]];
//高度=40
[superView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier: constant:]];
//view1底部距离superView底部距离为0
[superView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier: constant:]];

方法2.VFL方法:

//view1距离superview两端距离都为0
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view1]|" options: metrics:nil views:NSDictionaryOfVariableBindings(view1)]];
//view1高度为40,距离底端距离为0
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view1(==40)]" options: metrics:nil views:NSDictionaryOfVariableBindings(view1)]];
  • V:[view1][view2(==view1)]|
  • V表示竖直布局(vertical),先是一个view1,其下方紧接一个宽度等于view1宽度的view2,view2距离父视图底部边缘距离为0
  • H:|-[view1]-[view2]-[view3(>=20)]-|
  • H表示水平布局,view1距离父视图左边缘默认间隔宽度,之后是view2距离view1间隔默认宽度;再之后是宽度不小于20的view3,它和view2以及父视图右边缘的间距都是默认宽度。(竖线'|‘ 表示superview的边缘,“-|”表示默认距离,“|”表示距离边缘为0)

注意:

-(void)addConstraint:(NSLayoutConstraint *)constraint;

用来将约束添加到view。在添加时唯一要注意的是添加的目标view要遵循以下规则:

1.对于两个同层级view之间的约束关系,添加到他们的父view上

 

2对于两个不同层级view之间的约束关系,添加到他们最近的共同父view上

3对于有层次关系的两个view之间的约束关系,添加到层次较高的父view上

方法3.第三方库Masonry

    [view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.width.equalTo(superView);
        make.height.equalTo(@40);
        
    }];

//上下左右边距
-(void)masonryTest1{ UIView *blueView=[[UIView alloc]init];
blueView.backgroundColor=[UIColor blueColor];
[self.view addSubview:blueView]; UIView *redView=[[UIView alloc]init];
redView.backgroundColor=[UIColor redColor];
[self.view addSubview:redView]; [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.width.mas_equalTo(100);
// make.height.mas_equalTo(100);
// make.left.equalTo(self.view.mas_left).with.offset(10);
// make.top.equalTo(self.view.mas_top).with.offset(20); make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(, , , ));
}]; [redView mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(blueView.mas_height);
make.left.equalTo(blueView.mas_right).with.offset();
make.right.equalTo(self.view.mas_right).with.offset(-);
make.top.equalTo(blueView.mas_top).with.offset(); }]; }
// test2: 中心点与self.view相同,宽度为400*400
-(void)masonryTest2{
UIView *view = [UIView new];
[view setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.size.mas_equalTo(CGSizeMake(,));
}];
}

参考原文档:

http://blog.csdn.net/sxfcct/article/details/8776928

http://www.zhihu.com/question/37095424

masonry下载网址:

https://github.com/SnapKit/Masonry

纯代码Autolayout的三种方法的更多相关文章

  1. YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法

    上一篇介绍了 MVC中实现动态自定义路由 的实现,本篇将介绍Razor视图中以全局方式调用后台方法输出页面代码的三种方法. 框架最新的升级实现了一个页面部件功能,其实就是通过后台方法查询数据库内容,把 ...

  2. 编写Java程序_输入三个整数x,y,z,请把这三个数由小到大输出,请写出实现代码。(3种方法)

    要求说明: 输入三个整数x,y,z,请把这三个数由小到大输出. 实现代码: 第1种方法: import java.util.Scanner; public class xyzMaxMin{ publi ...

  3. Eclipse远程调试Java代码的三种方法

    Eclipse远程调试Java代码的三种方法, 第1种方法是用来调试已经启动的Java程序,Eclipse可以随时连接到远程Java程序进行调试, 第2种方法可以调试Java程序启动过程,但是Ecli ...

  4. 纯Css绘制三角形箭头三种方法

    在制作网页的过程中少不了绘制类似图片的三角形箭头效果,虽然工程量不大,但是确实麻烦.在学习的过程中,总结了以下三种方法,以及相关的例子. 一.三种绘制三角形箭头方法 1.方法一:利用overflow: ...

  5. Viewing the interface of your Swift code,查看Swift代码的头文件的三种方法

      Technical Q&A QA1914 Viewing the interface of your Swift code Q:  How do I view the interface ...

  6. Vue-Vue文本渲染三种方法 {{}}、v-html、v-text

    {{ }} 将元素当成纯文本输出 v-htmlv-html会将元素当成HTML标签解析后输出 v-textv-text会将元素当成纯文本输出 代码: <!DOCTYPE html> < ...

  7. Java实现ping功能的三种方法及Linux的区分

    前大半部份转自:https://blog.csdn.net/futudeniaodan/article/details/52317650 检测设备的运行状态,有的是使用ping的方式来检测的.所以需要 ...

  8. 像画笔一样慢慢画出Path的三种方法(补充第四种)

    今天大家在群里大家非常热闹的讨论像画笔一样慢慢画出Path的这种效果该如何实现. 北京-LGL 博客号@ligl007发起了这个话题.然后各路高手踊跃发表意见.最后雷叔 上海-雷蒙 博客号@雷蒙之星 ...

  9. JAVA之线程同步的三种方法

    最近接触到一个图片加载的项目,其中有声明到的线程池等资源需要在系统中线程共享,所以就去研究了一下线程同步的知识,总结了三种常用的线程同步的方法,特来与大家分享一下.这三种方法分别是:synchroni ...

随机推荐

  1. angular核心原理解析2:注入器的创建和使用

    上一课没有讲到创建注入器的方法createInjector. 此方法,会创建两种不同的注入器:第一种叫做providerInjector,第二种叫做instanceInjector.providerI ...

  2. python基础之循环

    一.while循环 如果条件成立(true),重复执行相同操作,条件不符合,跳出循环 while   循环条件: 循环操作 (1)while循环示例 例:输入王晓明5门课程的考试成绩,计算平均成绩 i ...

  3. 能够放在文档的 <head> 中的各种配置元素

    一份关于任何可以写入到你的文档中 <head> 部分的清单 最小推荐 <meta charset="utf-8"> <meta http-equiv= ...

  4. Centos7安装python3.7.1并与python2共存

    转自:http://www.cnblogs.com/JahanGu/p/7452527.html参考:https://www.jb51.net/article/104326.htm 1. 备份原来的p ...

  5. Mac拷贝/复制文件夹路径快捷键

    快捷键:Option+Command+C 显示路径在Finder: defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES ...

  6. Java异常机制关键字总结,及throws 和 throw 的区别

    在Java的异常机制中,时常出现五个关键字:try , catch , throw , throws , finally. 下面将总结各个关键字的用法,以及throw和throws的区别: (1) t ...

  7. 当spring抛出异常时出现的页面的@ExceptionHandler(RuntimeException.class) 用法

    当spring抛出异常时出现的页面的@ExceptionHandler(RuntimeException.class) 用法 主要用在Controller层 @ExceptionHandler(Run ...

  8. python 脚本备份 mysql 数据库到 OSS

    脚本如下: #!/usr/bin/python ########################################################### ################ ...

  9. 部署herko小记

    1 先执行如下 heroku run rake db:migrate

  10. java 位运算符,逻辑运算符

    逻辑运算符;布尔值时使用 a=true;b=false &: 逻辑或   例:a & b=false; |: 逻辑与   例:a | b=true; !:逻辑非    例:!a=fal ...