AutoLayout不管是在StoryBorad还是在xib中都相对来说比较简单,VFL(Visual  fromat  language)可视化语言基本上用到的比较少,在xCode4时候自动布局的概念还没有,直接使用VFL会很方便,可视化语言依赖于oc运行时创建对应的约束,如果IBOutlet发生改变有的时候会造成莫名其妙的Bug。xCode5之后可视化语言用到的场景相对较少,但是作为一个工作的辅助还是可以稍微了解下。

基础知识

在StotyBoard中添加一个标签一个按钮,不适用自动布局,简单的控制它们之间的水平距离为80,如下图所示:

视图中添加约束:

    NSLayoutConstraint  *labelContraint=[NSLayoutConstraint constraintWithItem:self.changeButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.descriptionLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:60];
[self.view addConstraint:labelContraint];

这个只是视图约束的一种方式,下面这种方式才是本文的主角:

    //使用可视化语言添加约束
NSDictionary *viewDictionary=NSDictionaryOfVariableBindings(_descriptionLabel,_changeButton);
NSArray *visualConstraint=[NSLayoutConstraint constraintsWithVisualFormat:@"[_descriptionLabel]-60-[_changeButton]" options:0 metrics:nil views:viewDictionary];
[self.view addConstraints:visualConstraint];

 这里面用到的constraintsWithVisualFormat方法,具体参数说明如下:

format:参数是vfl语句,语句的基本元素下面会详细解释一下;

opts:枚举参数,默认写0;

metrics:字典,当在format中使用了动态数据,会根据字典去匹配,接下来会具体有例子;

views:字典,传入需要用到的视图集合;

具体format需要参考一下表达式的意思:

水平方向          H:

垂直方向          V:

Views         [需要定义的视图]

SuperView      |

关系         >=,==,<=

间隙            -

视图内部约束           ()

Demo实战

通过VFL控制手动添加的标签的位置,具体效果如下:

代码实现如下:

    UILabel *link=[[UILabel alloc]init];
link.text=@"http://www.cnblogs.com/xiaofeixiang";
link.translatesAutoresizingMaskIntoConstraints=NO;
[link setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:link];
NSArray *horizontal=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[link]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(link)]; NSArray *vertical=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_descriptionLabel]-100-[link(>=30)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(link,_descriptionLabel)]; [self.view addConstraints:horizontal];
[self.view addConstraints:vertical];

 第一个约束是控制标签距离父视图左右之间的距离,第二个控制标签和”博客园-FlyElephant"之间的垂直距离为100.当然如果你想通过字典控制垂直之间的距离可以按照下面这么做:

    NSArray *vertical=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_descriptionLabel]-Vertical-[link(>=30)]" options:0 metrics:@{@"Vertical":@200} views:NSDictionaryOfVariableBindings(link,_descriptionLabel)];

 最后的结果:

友情提示在添加约束的时候不要和StoryBoard中的冲突,如果添加的水平约束StoryBoard中也有的话,就会出现下面这种情况:

2015-07-01 10:54:13.537 VFLDemo[2358:60863] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7fc5e3732860 H:[UILabel:0x7fc5e372ef30'\U535a\U5ba2\U56ed-FlyElephant']-(15)-[UIButton:0x7fc5e372d550'\U7fa4:228407086']>",
"<NSLayoutConstraint:0x7fc5e37344e0 H:[UILabel:0x7fc5e372ef30'\U535a\U5ba2\U56ed-FlyElephant']-(60)-[UIButton:0x7fc5e372d550'\U7fa4:228407086']>"
) Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fc5e37344e0 H:[UILabel:0x7fc5e372ef30'博客园-FlyElephant']-(60)-[UIButton:0x7fc5e372d550'群:228407086']> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

iOS开发-VFL(Visual format language)和Autolayout的更多相关文章

  1. 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    使用Auto Layout中的VFL(Visual format language)--代码实现自动布局 2014-12-09 10:56 编辑: zhiwupei 分类:iOS开发 来源:机智的新手 ...

  2. 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局【转】

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  3. 转载自@机智的新手:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  4. 【转】使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  5. 转载:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  6. 使用Auto Layout中的VFL(Visual format language)——代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:api介绍 1.NSLayoutConstraint API NSL ...

  7. IOS使用 Visual Format Language 定义水平和垂直约束

    定义限制条件来改变一个 UI 组件在其父视图的水平和垂直方向布局的方法. 可以使用方程式里 H:方向符号代表水平方向的边距,使用 V:方向符号代表垂直方向的边 距. 转载请注明,本文转自:http:/ ...

  8. Visual format language

    所谓的VFL语言其实就是Visual format language 的缩写,是一种使用代码添加约束的方式,类似于Masonry  SDAutolayout的效果,但是操作起来可能要相对简单.一行代码 ...

  9. iOS开发-VFL初窥

    VFL是苹果为了简化Autolayout的编码而推出的抽象语言,在上一篇博客中我们发现如果使用NSLayoutConstraint来添加约束是非常繁琐的. 一个简单的Frame需要添加四个NSLayo ...

随机推荐

  1. Android中动画

    两种动画 view动画 属性动画  (也可以使用xml描述动画) view 4动画 补间动画 渐变 AlphaAnimation 缩放 ScaleAnimation 平移 TranslateAnima ...

  2. jquery概要--基础01

    jquery对象,DOM对象 var $cr = $('#cr');          var cr = $cr[0]; /var cr = $cr.get(0); var cr = document ...

  3. Xamarin如何生成Android项目的APK

    Xamarin如何生成Android项目的APK 首先需要选择Release模式生成项目.然后从“生成”菜单中选择Export Android Package命令,就可以导出APK包.APK保存在An ...

  4. css构造文本

    1. 1. 文本缩进text-indent:值:值为数字,最常用的数值单位是px(像素),也可以直接是百分比!text-indent:100px;text-indent:10%;2. 文本对齐text ...

  5. HDU3037 Saving Beans(Lucas定理+乘法逆元)

    题目大概问小于等于m个的物品放到n个地方有几种方法. 即解这个n元一次方程的非负整数解的个数$x_1+x_2+x_3+\dots+x_n=y$,其中0<=y<=m. 这个方程的非负整数解个 ...

  6. BZOJ1525 : [POI2006]Zos

    由于k很小,所以随机一组解的正确率有90%以上. 每次随机选取一个没被删除的点,然后将与其相邻的点都删去即可. #include<cstdio> #include<cstdlib&g ...

  7. vim molokai配色方案(调过)

    " Vim color file " " Author: Tomas Restrepo <tomas@winterdom.com> " " ...

  8. 一、saltstack简介和安装

    系统环境:CentOS6.5 准备yum源: epel源(包含了saltstack的包).阿里源(CentOS-Base.repo) Host解析文件: # cat /etc/hosts 192.16 ...

  9. 插入随机数到MySQL数据库

    我们经常会遇到使用随机的问题,下面就是一种解决随机数的方法. 在构造测试数据时,我们需要对测试表插入随机数据.构造测试数据的方法如下,仅以update为例说明 步骤1:随机数的SQL函数为rand() ...

  10. Nginx 笔记与总结(5)访问日志管理:计划任务 + 日志切割

    要在第二天的凌晨把前一天的访问日志切割备份,并以时间作为文件名,例如:access.20150728.log,这就需要在 Linux 中格式化时间,例如: [root@localhost ~]# da ...