什么是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 UI布局-VFL语言的更多相关文章

  1. iOS UI布局调试工具

    查看ios软件的ui布局有三种: 1.DCIntrospect    这种方式是开源的,我从github上clone下来后运行demo,运行遇到了问题:Xcode cannot run using t ...

  2. iOS学习之VFL语言简介

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

  3. iOS UI布局-定时器

    定时器是比较常用的一个UI控件,在付款.抢购时,经常会使用到.提取成一个通用的方法 /** * 倒计时GCD通用方法 * 通常用的计时器都是用NSTimer,但是NSTimer在线程很吃紧的时候效果不 ...

  4. iOS UI布局总结

    布局就是尺寸和位置的设置. 一.基本布局: 1)绝对布局:frame.layoutsubviews. 二.相对布局: autoresizing.autolayout.基于父视图.基于约束. 三.线性布 ...

  5. iOS UI布局-回到顶部

    回到顶部,是比较常用的一个效果 核心代码 在ViewDidLoad中,添加回到顶部按钮 计算偏移量,如果当前显示的内容全部向上隐藏掉,则显示“回到顶部”按钮 // // ViewController. ...

  6. IOS VFL语言(页面布局)

    ● 什么是VFL语言 ● VFL全称是Visual Format Language,翻译过来是“可视化格式语言” ● VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言     VFL ...

  7. iOS开发~UI布局(三)深入理解autolayout

    一.概要 通过对iOS8界面布局的学习和总结,发现autolayout才是主角,autolayout是iOS6引入的新特性,当时还粗浅的学习了下,可是没有真正应用到项目中.随着iOS设备尺寸逐渐碎片化 ...

  8. iOS,自动布局autoresizing和auto layout,VFL语言

    1.使用autoresizing 2.使用autolayout 3.VFL语言(Visual Format Language:可视化格式语言) 使用autoresizing 点击xib文件,去掉使用a ...

  9. iOS开发~UI布局(二)storyboard中autolayout和size class的使用详解

    一.概要:前一篇初步的描述了size class的概念,那么实际中如何使用呢,下面两个问题是我们一定会遇到的: 1.Xcode6中增加了size class,在storyboard中如何使用? 2.a ...

随机推荐

  1. css学习_css三大特性

    css三大特性 1.层叠性(就近原则) 2.继承性(和文字有关的会继承) 3.优先级   (权重问题) 权重:0,0,0,0 0001 ---标签选择器(注意:即使有20个标签选择器也不会比一个伪类选 ...

  2. [No0000154]详解为什么32位系统只能用4G内存.

    既然是详解, 就从最基础的讲起了. 或者1来存储数据的, 所以Bit实际上可以看成存放1个二进制数字的1个位置.也就是说bit只有2种值, 0 或者 1, 所以1个bit能存放1个布尔类型的值(boo ...

  3. Python使用xml.dom解析xml

    在菜鸟教程上找了个关于电影信息的xml类型的文档,用python内置的xml.dom来解析提取一下信息. 先复习一下xml概念: XML 指可扩展标记语言(EXtensible Markup Lang ...

  4. python与pip安装

    # Install pip for 2.7 and then python 2.7 itself sudo apt install python-pip sudo apt install python ...

  5. gateone安装使用

    下载地址 https://github.com/liftoff/GateOne unzip GateOne-master.zip cd GateOne-master/ python setup.py ...

  6. spring相关的maven依赖

    <properties> <springframework.version>5.0.4.RELEASE</springframework.version> < ...

  7. xadmin与django-rest-framework的集成(1)

    什么是xadmin?什么是django-rest-framework? xadmin是开源的一个类似于django自带的后台管理系统admin的开源模块,它基于bootstrap3框架,内置强大的插件 ...

  8. linux链接库的理解

    前段时间遇到个奇怪的问题,经调试是由于可执行程序A编译时使用的libssl.so.1.1及对应版本头文件,A链接的库libtest.so编译时使用的libssl.so.1.0及对应版本头文件,执行时l ...

  9. websocketd

    https://www.cnblogs.com/tinywan/p/6826125.html https://www.jianshu.com/p/63afd0099565

  10. 前端 HTML body标签相关内容 常用标签 分割线 <hr>

    分割线 <hr> <hr>标签用来在HTML页面中创建水平分隔线,通常用来分隔内容 <!DOCTYPE html> <html lang="en&q ...