一.什么是VFL语言?为什么要VFL语言?
VFL全称是Visual Format Language,翻译过来是“可视化格式语言”
VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言
代码分析:
    NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:<#(NSString *)#> options:<#(NSLayoutFormatOptions)#> metrics:<#(NSDictionary *)#> views:<#(NSDictionary *)#>]
VisualFormat:VFL语句
options:约束类型
metrics :VFL语句中用到的具体数值
views :VFL语句中用到的控件
 
二.例子
1.简单VFL
 - (void)viewDidLoad{
2 [super viewDidLoad];
3 UIView *redView = [[UIView alloc]init];
4 redView.backgroundColor = [UIColor redColor];
5 redView.translatesAutoresizingMaskIntoConstraints= NO;
[self.view addSubview:redView];
     UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints= NO;
[self.view addSubview:blueView];
//水平方向
NSString *hVFL=@"H:|-20-[redView]-30-[blueView(==redView)]-20-|";
NSArray *hCons =[NSLayoutConstraint constraintsWithVisualFormat:hVFL options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:@{@"redView":redView,@"blueView":blueView}];
[self.view addConstraints:hCons];
//垂直方向
NSString *vVFL =@"V:|-20-[redView(50)]";
NSArray *vCons =[NSLayoutConstraint constraintsWithVisualFormat:vVFL options: metrics:nil views:@{@"redView":redView}];
[self.view addConstraints:vCons];
}

效果:

2. 复杂VFL无法完整表示 必须结合 constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:. 一起使用

 - (void)viewDidLoad{
2 [super viewDidLoad];
UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints= NO;
[self.view addSubview:blueView];
     UIView *redView = [[UIView alloc]init];
redView.backgroundColor = [UIColor redColor];
redView.translatesAutoresizingMaskIntoConstraints= NO;
[self.view addSubview:redView];
//水平方向
NSString *hVFL =@"H:|-30-[blueView]-30-|";
NSArray *hCons = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options: metrics:nil views:@{@"blueView":blueView}];
[self.view addConstraints:hCons];
//垂直方向
NSString *vVFL =@"V:|-30-[blueView(50)]-20-[redView(==blueView)]";
NSArray *vCons = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView":blueView,@"redView":redView}];
[self.view addConstraints:vCons]; //VFL无法描述 redView 左边 和 blueViwe 中心对齐
NSLayoutConstraint *redLeftCon = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
[self.view addConstraint:redLeftCon];
}

//注意 :官方文档中
The notation prefers good visualization over completeness of expressibility. Most of the constraints that are useful in real user interfaces can be expressed using visual format syntax, but there are a few that cannot. One useful constraint that cannot be expressed is a fixed aspect ratio (for example, imageView.width = 2 * imageView.height). To create such a constraint, you must use

constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:.

效果:

AutoLayout自动布局之VFL语言代码实现(一个神奇的语言)的更多相关文章

  1. HTML-参考手册: HTML 语言代码

    ylbtech-HTML-参考手册: HTML 语言代码 1.返回顶部 1. HTML 语言代码 参考手册 ISO 语言代码 HTML 的 lang 属性可用于声明网页或部分网页的语言.这对搜索引擎和 ...

  2. C语言编译器为什么能够用C语言编写?

    不知道大家有没有想过一个问题:C语言编译器为什么能够用C语言编写? 所谓C语言编译器,就是把编程得到的文件,比如.c,.h的文件,进行读取,并对内容进行分析,按照C语言的规则,将其转换成cpu可以执行 ...

  3. 【C语言】03-第一个C程序代码分析

    前面我们已经创建了一个C程序,接下来分析一下里面的代码. 项目结构如下: 一.代码分析 打开项目中的main.c文件(C程序的源文件拓展名为.c),可以发现它是第一个C程序中的唯一一个源文件,代码如下 ...

  4. 【C语言】01-第一个c程序代码分析

    创建了一个C程序,接下来分析一下里面的代码. 项目结构如下: 一.代码分析 打开项目中的main.c文件(C程序的源文件拓展名为.c),可以发现它是第一个C程序中的唯一一个源文件,代码如下: 1 #i ...

  5. AutoLayout(自动布局)

    1. iOS两种自适应布局方式:(修正说明:) -AutoLayout(自动布局) + SizeClasses(尺寸类别) -Autoresizing (自动调整尺寸/弹簧式调整尺寸) 前者 Auto ...

  6. iOS自动布局高级用法 && 纯代码约束写法

    本文主要介绍几个我遇到的总结的高级用法(当然我相信肯定有不少比这还高级的). 简单的storyboard中上下左右约束,固定宽高啥的用法在这里就不做赘述了. autolayout自动布局是iOS6以后 ...

  7. iOS:Autolayout自动布局实例

    Autolayout自动布局实例即可以用故事板布局,也可以用纯代码创建,各有各的优点:用故事板布局,比较方便,而且直观,可以很直白的看到视图布局后的变化:采用代码布局,虽然代码比较多,有些麻烦,但是可 ...

  8. Latex中插入C语言代码

    Latex是一个文本排版的语言,能排版出各种我们想要的效果.而且用代码排版的优点是易于修改板式,因此在文本内容的排版时,Latex应用十分广泛. 当我们需要在Latex中插入代码时,就需要用到 \us ...

  9. loadrnner添加C语言代码的几种方式

    今天有人在群里问,想直接把自己的C语言代码让lr调用,该怎么搞. 这东西说来简单,只是对Loadrunner这工具不熟悉可能才会有这种问题吧.个人理解,一般有三种方法吧,废话不多,直接干货. 1.直接 ...

随机推荐

  1. 常见26个jquery使用技巧详解

    本文列出jquery一些应用小技巧,比如有禁止右键点击.隐藏搜索文本框文 字.在新窗口中打开链接.检测浏览器.预加载图片.页面样式切换.所有列等高.动态控制页面字体大小.获得鼠标指针的X值Y值.验证元 ...

  2. w3cschool在线教程

    做网页开发的,没有不知道w3cschool的,如果你还不知道,那么就应该早点看下面推荐的文章,菜鸟可以帮你提升你的技能,老鸟可以温故而知新. 第一个是:http://www.w3school.com. ...

  3. PCB 锣板和半孔工艺的差别

    PCB 锣板和半孔工艺的差别 PCB 在做模块时会用到半孔工艺,但是由于半孔是特殊工艺. 需要加费用,打板时费还不低. 下面这个图是锣板和半孔工艺的差别. https://www.amobbs.com ...

  4. 程序4-4 chmod函数实例

    //http://blog.chinaunix.net/uid-24549279-id-71355.html /* ========================================== ...

  5. RK3288 dts文件中背光配置参数

    backlight { // 设备名(用于匹配) compatible = "pwm-backlight"; // pwm编号 通道 频率(ns) pwms = <& ...

  6. 有关Botton的用法(一)

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content&qu ...

  7. erlang的tcp服务器模板

    改来改去,最后放github了,贴的也累,蛋疼 还有一个tcp批量客户端的,也一起了 大概思路是 混合模式 使用erlang:send_after添加recv的超时处理 send在socket的opt ...

  8. awk常见基本使用

    -F 指定分割符号 print 外层的引号必须是单引号 $n不能被解析 [root@bogon ~]# .txt a:b:c:d a1:b1:c1:d1 a2:b2:c2:d2 a_: :c:dddd ...

  9. C# 32位程序访问64位注册表

    接上文:http://www.cnblogs.com/TaiYangXiManYouZhe/p/5086974.html 上代码: RegistryKey localKey; if (Environm ...

  10. 1048 Find Coins

    题意:略 思路:two pointers思想,简单 先对数字序列进行排序,然后定义两个指针left和right,初始状态low=0,high=n-1.当a[low]+a[high] > M时,h ...