一谈到Autolayout,初学者肯定想到的是IB中使用拖拽啊,pin啊各种鼠标操作来进行添加各种约束。

今天我们要聊得是如何利用代码来添加视图间的约束。

我们来看一个例子:

(Objective-C代码)

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v1.backgroundColor = [UIColor colorWithRed: green:. blue: alpha:];
UIView* v2 = [UIView new];
v2.backgroundColor = [UIColor colorWithRed:. green: blue: alpha:];
UIView* v3 = [UIView new];
v3.backgroundColor = [UIColor colorWithRed: green: blue: alpha:];
[mainview addSubview: v1];
[v1 addSubview: v2];
[v1 addSubview: v3]; v2.translatesAutoresizingMaskIntoConstraints = NO;
v3.translatesAutoresizingMaskIntoConstraints = NO; [v1 addConstraint:
[NSLayoutConstraint
constraintWithItem:v2 attribute:NSLayoutAttributeLeft
relatedBy:
toItem:v1 attribute:NSLayoutAttributeLeft
multiplier: constant:]]; [v1 addConstraint:
[NSLayoutConstraint
constraintWithItem:v2 attribute:NSLayoutAttributeRight
relatedBy:
toItem:v1 attribute:NSLayoutAttributeRight
multiplier: constant:]]; [v1 addConstraint:
[NSLayoutConstraint
constraintWithItem:v2 attribute:NSLayoutAttributeTop
relatedBy:
toItem:v1 attribute:NSLayoutAttributeTop
multiplier: constant:]]; [v2 addConstraint:
[NSLayoutConstraint
constraintWithItem:v2 attribute:NSLayoutAttributeHeight
relatedBy:
toItem:nil attribute:
multiplier: constant:]]; [v3 addConstraint:
[NSLayoutConstraint
constraintWithItem:v3 attribute:NSLayoutAttributeWidth
relatedBy:
toItem:nil attribute:
multiplier: constant:]]; [v3 addConstraint:
[NSLayoutConstraint
constraintWithItem:v3 attribute:NSLayoutAttributeHeight
relatedBy:
toItem:nil attribute:
multiplier: constant:]]; [v1 addConstraint:
[NSLayoutConstraint
constraintWithItem:v3 attribute:NSLayoutAttributeRight
relatedBy:
toItem:v1 attribute:NSLayoutAttributeRight
multiplier: constant:]]; [v1 addConstraint:
[NSLayoutConstraint
constraintWithItem:v3 attribute:NSLayoutAttributeBottom
relatedBy:
toItem:v1 attribute:NSLayoutAttributeBottom
multiplier: constant:]];

(Swift代码 iOS9)

let v1 = UIView(frame:CGRectMake(, , , ))
v1.backgroundColor = UIColor(red: , green: 0.4, blue: , alpha: )
let v2 = UIView()
v2.backgroundColor = UIColor(red: 0.5, green: , blue: , alpha: )
let v3 = UIView()
v3.backgroundColor = UIColor(red: , green: , blue: , alpha: )
mainview.addSubview(v1)
v1.addSubview(v2)
v1.addSubview(v3) v2.translatesAutoresizingMaskIntoConstraints = false
v3.translatesAutoresizingMaskIntoConstraints = false v1.addConstraint(
NSLayoutConstraint(item: v2,
attribute: .Leading,
relatedBy: .Equal,
toItem: v1,
attribute: .Leading,
multiplier: , constant: )
) v1.addConstraint(
NSLayoutConstraint(item: v2,
attribute: .Trailing,
relatedBy: .Equal,
toItem: v1,
attribute: .Trailing,
multiplier: , constant: )
) v1.addConstraint(
NSLayoutConstraint(item: v2,
attribute: .Top,
relatedBy: .Equal,
toItem: v1,
attribute: .Top,
multiplier: , constant: )
) v2.addConstraint(
NSLayoutConstraint(item: v2,
attribute: .Height,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: , constant: )
) v3.addConstraint(
NSLayoutConstraint(item: v3,
attribute: .Width,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: , constant: )
) v3.addConstraint(
NSLayoutConstraint(item: v3,
attribute: .Height,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: , constant: )
) v1.addConstraint(
NSLayoutConstraint(item: v3,
attribute: .Trailing,
relatedBy: .Equal,
toItem: v1,
attribute: .Trailing,
multiplier: , constant: )
) v1.addConstraint(
NSLayoutConstraint(item: v3,
attribute: .Bottom,
relatedBy: .Equal,
toItem: v1,
attribute: .Bottom,
multiplier: , constant: )
)

运行效果:

(竖屏)

(横屏)

看了以上代码后,你肯定要疯了,那么多约束。。。

下面,我们就来逐一分析:

我们先来看一下这段代码

OC

v3 = [[UIView alloc] initWithFrame:CGRectMake(v1.bounds.size.width-,
v1.bounds.size.height-,
, )];

Swift

let v3 = UIView(frame:CGRectMake(
v1.bounds.width-, v1.bounds.height-, , ))

这段代码很清楚地表达了:v3是宽高各20,并且位置在v1的右下角,其原点距离v1的右下角

坐标x,y各偏移20,也就是我们上图中看到的大红色矩形。

约束的API语句有时候是很冗长的,看上去很难懂。

为此,Apple发明了可视化格式(Visual Format)来便于理解。

看看下面的这个例子:

@"V:|[v2(10)]"

上面的表达式中,V:表示是垂直方向上的约束,同理,H:表示水平方向上约束。

管道符|代表父视图。

中括号内是要添加约束的视图变量名。

所以,这里的约束清晰地表达: v2和父视图顶端对齐,并且v2的高度是10。

iOS编程(双语版)-视图-Autolayout代码初步的更多相关文章

  1. iOS编程(双语版) - 视图 - 手工代码(不使用向导)创建视图

    如何创建一个空的项目,最早的时候XCode的项目想到中,还有Empty Application template这个选项,后来Apple把它 给去掉了. 我们创建一个单视图项目. 1) 删除main. ...

  2. iOS编程(双语版) - 视图 - 基本概念

    1. 什么是视图? 视图显示为手机上的一块矩形区域,管理该区域的所有屏幕显示,它是UIView或者UIView的子类. 视图既可以从xib生成,也可以用代码生成. 2. 窗口 窗口是UIWindow或 ...

  3. iOS编程(双语版)-视图-Frame/Bounds/Center

    1. Frame 每个视图都有一个frame属性,它是CGRect结构,它描述了视图所在的矩形在其父视图中的位置. (屏幕坐标系默认的原点在左上角,x轴向右伸展,y轴向下伸展) 设置frame通常通过 ...

  4. iOS编程(双语版) - 视图 - Transform(转换)

    视图有一个transform属性,它描述了应该如何绘制该视图. 该属性是CGAffineTransform结构体,它代表了3 x 3的变换矩阵(线性代数). 下面的代码让两个矩形视图旋转45度 (Ob ...

  5. iOS编程——经过UUID和KeyChain来代替Mac地址实现iOS设备的唯一标示(OC版)

    iOS编程——通过UUID和KeyChain来代替Mac地址实现iOS设备的唯一标示(OC版) 很多的应用都需要用到手机的唯一标示,而且要求这个唯一标示不能因为应用app的卸载或者改变而变化. 在iO ...

  6. 新书《iOS编程(第6版)》抢鲜试读

    我最近翻译了Big Nerd Ranch的<iOS编程(第6版)>.我用了大半年时间,尽可能做到通顺易懂.不足之处请大家多多指正.感谢辛苦审校的丁道骏同学. 这本书得过Jolt大奖,原书在 ...

  7. IOS编程User Interface基础

    IOS编程之User Interface基础 目录 概述 相关概念 常见问题 状态栏的隐藏 应用图标的设置 概述 IOS用户界面是APP呈现给用户最直观.最常用的方式,因此学会用户界面的编程是学习IO ...

  8. python核心编程第二版笔记

    python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d   提供调试输出1.2 –O   生成优化的字节码(生成 ...

  9. iOS,Core Animation--负责视图的复合功能

    简介 UIKit API UIKit是一组Objective-C API,为线条图形.Quartz图像和颜色操作提供Objective-C 封装,并提供2D绘制.图像处理及用户接口级别的动画.    ...

随机推荐

  1. java根据模板导出PDF详细教程

    原文:https://blog.csdn.net/pengyufight/article/details/75305128 题记:由于业务的需要,需要根据模板定制pdf文档,经测试根据模板导出word ...

  2. NSString 拼接字符串

    NSString* string; // 结果字符串 NSString* string1, string2; //已存在的字符串,需要将string1和string2连接起来 //方法1. strin ...

  3. Safari支不支持HTML5录音? 现在浏览器中最好的解决方案是WebRTC下的 navigator.getUserMedia API。

    先放结论:Safari支不支持HTML5录音? ——据我调查,不支持. 现在浏览器中最好的解决方案是WebRTC下的 navigator.getUserMedia API. 可是当使用Can I us ...

  4. Android之录音工具类

    /** * 录音工具类 * * @author rendongwei * */ public class RecordUtil { private static final int SAMPLE_RA ...

  5. Java命令学习系列(六)——jinfo

    jinfo可以输出java进程.core文件或远程debug服务器的配置信息.这些配置信息包括JAVA系统参数及命令行参数,如果进程运行在64位虚拟机上,需要指明-J-d64参数,如:jinfo -J ...

  6. Chapter 1 -- UsingAndAvoidingNull

    "Null sucks." -Doug Lea "Null 很恶心!" "I call it my billion-dollar mistake.&q ...

  7. Intellij IDEA打开就闪退或关闭

    找到idea安装目录的bin目录,搜索vmoptions可以看到两个文件, idea.exe.vmoptions idea64.exe.vmoptions 1 这两个文件就是IDEA的一些配置文件,带 ...

  8. 要恢复页面吗?Chrome未正确关闭

    谷歌chrome浏览器每次打开提示"要恢复页面吗"怎么办? 谷歌chrome浏览器每次打开提示"要恢复页面吗"怎么办? 如下图所示: 每次打开启动谷歌chrom ...

  9. maven-shade-plugin 入门指南

    1. Why? 通过 maven-shade-plugin 生成一个 uber-jar,它包含所有的依赖 jar 包. 2. Goals Goal Description shade:help Dis ...

  10. C++ 变量默认初始值不确定(代码测试)

    C++ int变量默认初始值是不确定的,因此使用时初始化是很有必要的. 下面写个小程序测试一下int变量默认初始值. #include <iostream> #include <ve ...