什么是VFL语言

VFL(Visual Format Language),“可视化格式语言”。

VFL是苹果公司为了简化autolayout的编码而推出的抽象语言。

语法说明

  1. H:[cancelButton()]--[acceptButton()]
  2. cancelButton72acceptButton50,它们之间间距12
  3.  
  4. H:[wideView(>=@)]
  5. wideView宽度大于等于60point,该约束条件优先级为700(优先级最大值为1000,优先级越高的约束条件越先被满足)
  6.  
  7. V:[redBox][yellowBox(==redBox)]
  8. 垂直方向上,先有一个redBox,其下方紧接一个高度等于redBox高度的yellowBox
  9.  
  10. H:|--[Find]-[FindNext]-[FindField(>=)]-|
  11. 水平方向上,Find距离父view左边缘间隔10,之后是FindNext距离Find间隔默认宽度;再之后是宽度不小于20FindField,它和FindNext以及父view右边边缘的间距都是默认宽度。(竖线“|”表示superview的边缘)。

使用方法

  1. 使用VFL来创建约束数组
  2. +(NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;
  3.  
  4. formatVFL语句
  5. opts:约束类型
  6. metricsVFL语句中用到的具体数值
  7. viewsVFL语句中用到的控件
  8.  
  9. 创建一个字典(内部包含VFL语句中用到的控件)的快捷宏定义
  10. NSDictionaryOfVariableBindings(...)

实例展示

效果图如下:

             

实现代码:

  1. -(void)horizontalLayout{
  2. //1.添加两个控件
  3. UIView *blueView = [[UIView alloc] init];
  4. blueView.backgroundColor = [UIColor blueColor];
  5.  
  6. blueView.translatesAutoresizingMaskIntoConstraints = NO;
  7. [self.view addSubview:blueView];
  8.  
  9. UIView *redView = [[UIView alloc] init];
  10. redView.backgroundColor = [UIColor redColor];
  11. redView.translatesAutoresizingMaskIntoConstraints = NO;
  12. [self.view addSubview:redView];
  13.  
  14. //2.添加约束
  15. //2.1水平方向的约束
  16. NSString *hVFL = @"H:|-30-[blueView]-30-[redView(==blueView)]-30-|";
  17. NSArray *hCons = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options:NSLayoutFormatAlignAllBottom | NSLayoutFormatAlignAllTop metrics:nil views:@{@"blueView":blueView, @"redView":redView}];
  18. [self.view addConstraints:hCons];
  19.  
  20. //2.2垂直方向的约束
  21. NSString *vVFL = @"V:[blueView(50)]-30-|";
  22. NSArray *vCons = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options: metrics:nil views:@{@"blueView":blueView}];
  23. [self.view addConstraints:vCons];
  24. }
  25.  
  26. -(void)verticalLayout{
  27. //1.添加两个控件
  28. UIView *blueView = [[UIView alloc] init];
  29. blueView.backgroundColor = [UIColor blueColor];
  30. blueView.translatesAutoresizingMaskIntoConstraints = NO;
  31. [self.view addSubview:blueView];
  32.  
  33. UIView *redView = [[UIView alloc] init];
  34. redView.backgroundColor = [UIColor redColor];
  35. redView.translatesAutoresizingMaskIntoConstraints = NO;
  36. [self.view addSubview:redView];
  37.  
  38. //2.添加约束
  39. //2.1水平方向的约束
  40. NSString *hVFL = @"H:|-30-[blueView]-30-|";
  41. NSArray *hCons = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options: metrics:nil views:@{@"blueView":blueView}];
  42. [self.view addConstraints:hCons];
  43.  
  44. //2.2垂直方向的约束
  45. NSString *vVFL = @"V:|-30-[blueView(50)]-30-[redView(==blueView)]";
  46. NSArray *vCons = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView":blueView, @"redView":redView}];
  47. [self.view addConstraints:vCons];
  48. NSLayoutConstraint *redLeftCon = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:];
  49. [self.view addConstraint:redLeftCon];
  50. }

小结

最后对格式的字符串作一个总结介绍:

功能 表达式
水平方向 H:
垂直方向 V:
Views [view]
SuperView |
关系 >=,==,<=
空间,间隙 -
优先级 @value

iOS学习之VFL语言简介的更多相关文章

  1. IOS学习笔记07---C语言函数-printf函数

    IOS学习笔记07---C语言函数-printf函数 0 7.C语言5-printf函数 ------------------------- ----------------------------- ...

  2. IOS学习笔记06---C语言函数

    IOS学习笔记06---C语言函数 --------------------------------------------  qq交流群:创梦技术交流群:251572072              ...

  3. iOS UI布局-VFL语言

    什么是VFL语言 VFL(Visual Format Language),“可视化格式语言”. VFL是苹果公司为了简化autolayout的编码而推出的抽象语言. 语法说明 H:[cancelBut ...

  4. Linux C编程学习之C语言简介---预处理、宏、文件包含……

    C的简介 C语言的结构极其紧凑,C语言是一种模块化的编程语言,整个程序可以分割为几个相对独立的功能模块,模块之间的相互调用和数据传递是非常方便的 C语言的表达能力十分强大.C语言兼顾了高级语言和汇编语 ...

  5. iOS学习笔记---oc语言第一天

    第一讲 初始类和对象 c语言的超集,允许在oc中使用c语言源代码.编译器兼容c语言程序 具备完善的面向对象特性 包含一个运行时系统 类库丰富 面向对象编程 oop 面向对象语言:c++  java   ...

  6. iOS学习笔记---c语言第八天

    指针 首先将变量a的地址存放在另一个变量中,比如存放在变量b中,然后通过变量b来间接引用变量a,间接读写变量a的值.用来存放变量地址的变量,就称为"指针变量" int *p=nul ...

  7. iOS学习笔记---oc语言第九天

    初级内存管理 iOS应用程序出现crash(闪退),90%以上是内存问题////其他:数组越界,方法只声明没实现 内存问题体现在两个方面:内存溢出\野指针异常 内存溢出:程序运行超出内存上限 野指针异 ...

  8. iOS学习笔记---oc语言第六天

    Block .数组高级 block本质上就是匿名函数(没有名称的函数) block语法和函数指针很相似 回顾函数 函数:C语⾔中,实现某一类功能的代码段. 完整的函数包含两部分:函数声明.函数定义 函 ...

  9. iOS学习笔记---oc语言第五天

    字典,数组 ,集排序 一.字典类 存储以下数据 name:张三; sex:男;age:18 film:风暴; playcount:10000次;price:60元 字典类用于保存具有映射关系(key- ...

随机推荐

  1. LeetCode——Nim Game

    Description: You are playing the following Nim Game with your friend: There is a heap of stones on t ...

  2. C#生成随机验证码例子

    C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...

  3. Android6.0中PowerManagerService分析

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=30510400&id=5569393 概述 一直以来,电源管理是 ...

  4. [原]git的使用(六)---远程仓库

    10.远程仓库 -------------------------------------------------------------------------------------------- ...

  5. 【POJ2888】Magic Bracelet Burnside引理+欧拉函数+矩阵乘法

    [POJ2888]Magic Bracelet 题意:一个长度为n的项链,有m种颜色的珠子,有k个限制(a,b)表示颜色为a的珠子和颜色为b的珠子不能相邻,求用m种珠子能串成的项链有多少种.如果一个项 ...

  6. C++语言的学习环境

    一.Mac C++语言的学习环境 1. 1.1.C语言 :终端->bash-vi/vim文本编辑器->GNU->GCC文件编译器->a.out可执行文件 1.2.C++语言:终 ...

  7. html、css如何画实心圆

    css3画实心圆 实现方法相当简单,css代码如下: .circle{ width:100px; height:100px; border-radius:50px; /* 图形的半径 */ }

  8. Docker添加镜像加速器

    Docker默认pull连接镜像为国外镜像,速度较慢,注册阿里云可以生成一个镜像加速器 登录阿里云 https://cr.console.aliyun.com获取私有加速地址 修改配置文件/etc/d ...

  9. 152 - - G Traffic Light 搜索(The 18th Zhejiang University Programming Contest Sponsored by TuSimple )

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5738 题意 给你一个map 每个格子里有一个红绿灯,用0,1表示 ...

  10. AmazonOrder xml web语义化

    XML Processing Modules — Python 3.7.1 documentation https://docs.python.org/3.7/library/xml.html#xml ...