1、添加约束的规则

  • 在创建约束之后,需要将其添加到作用的view上
  • 在添加时要注意目标view需要遵循以下规则:
  1)对于 两个同层级view之间 的约束关系,添加到它们的父view上  
  2)对于 两个不同层级view之间 的约束关系,添加到他们最近的共同父view上
  3)对于 有层次关系的两个view之间 的约束关系,添加到层次较高的父view上
 
2、苹果原生代码实现Autolayout
  • 步骤

  1)利用NSLayoutConstraint类创建具体的约束对象

   1> 一个NSLayoutConstraint对象就代表一个约束

   2> 创建约束对象的常用方法      

+(id)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

   参数说明

    pview1 :要约束的控件

      pattr1 :约束的类型(做怎样的约束)

      prelation :与参照控件之间的关系

      pview2 :参照的控件

      pattr2 :约束的类型(做怎样的约束)

      pmultiplier :乘数

      pc :常量

  2)添加约束对象到相应的view上

    - (void)addConstraint:(NSLayoutConstraint *)constraint;

    - (void)addConstraints:(NSArray *)constraints;

  • 注意点

  1)要先禁止autoresizing功能,设置view的下面属性为NO

   view.translatesAutoresizingMaskIntoConstraints = NO;

   如果不设置为NO,系统会自动将AutoresizingMask转为Autolayout的约束,从而造成约束冲突,控制台打印内容如下:

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)

(

"<NSAutoresizingMaskLayoutConstraint:0x7ff339620540 h=--& v=--& V:[UIView:0x7ff339714510(0)]>",

"<NSLayoutConstraint:0x7ff339714990 V:[UIView:0x7ff339714510(40)]>"

)

Will attempt to recover by breaking constraint

<NSLayoutConstraint:0x7ff339714990 V:[UIView:0x7ff339714510(40)]>

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.

  2)添加约束之前,一定要保证相关控件都已经在各自的父控件上,也就是将子控件添加到父控件上

  3)不用再给view设置frame

  • 代码: 
   UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
// 不要将AutoresizingMask转为Autolayout的约束
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView]; UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
// 不要将AutoresizingMask转为Autolayout的约束
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView];// 添加宽度约束:100 /************************** 蓝色 **************************/
// 添加高度约束:40
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:];
[blueView addConstraint:heightConstraint]; // 添加左边约束:blueView的左边距离父控件左边有20的间距
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:];
[self.view addConstraint:leftConstraint]; // 添加右边约束:blueView的右边距离父控件右边有20的间距
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-];
[self.view addConstraint:rightConstraint]; // 添加顶部约束:blueView的顶部距离父控件顶部有20的间距
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:];
[self.view addConstraint:topConstraint]; /************************** 红色 **************************/
// 添加高度约束:蓝色等高
NSLayoutConstraint *heightConstraint2 = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:];
[self.view addConstraint:heightConstraint2]; // 添加左边约束:redView的左边 == 父控件的中心x
NSLayoutConstraint *leftConstraint2 = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:];
[self.view addConstraint:leftConstraint2]; // 添加顶部约束:redView的顶部距离blueView的底部有20的间距
NSLayoutConstraint *topConstraint2 = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:];
[self.view addConstraint:topConstraint2]; // 添加右边约束:redView的右边 == blueView的右边
NSLayoutConstraint *rightConstraint2 = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:];
[self.view addConstraint:rightConstraint2];

    效果图如下:

3、VFL语言

  • 什么是VFL语言

  VFL全称是Visual Format Language,翻译过来是“可视化格式语言”

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

  • VFL示例:

  1)H:[cancelButton(72)]-12-[acceptButton(50)]

  canelButton宽72,acceptButton宽50,它们之间间距12

  2)H:[wideView(>=60@700)]

  wideView宽度大于等于60point,该约束条件优先级为700(优先级最大值为1000,优先级越高的约束越先被满足)

  3)V:[redBox][yellowBox(==redBox)]

  竖直方向上,先有一个redBox,其下方紧接一个高度等于redBox高度的yellowBox

  4)H:|-10-[Find]-[FindNext]-[FindField(>=20)]

  水平方向上,Find距离父view左边缘默认间隔宽度,之后是FindNext距离Find间隔默认宽度;再之后是宽度不小于20的FindField,它和FindNext以及父view右边缘的间距都是默认宽度。(竖线“|” 表示superview的边缘)

  • VFL的使用

  1)使用VFL来创建约束数组方法

+ (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;
   format :VFL语句
   opts :约束类型
   metrics :VFL语句中用到的具体数值
   views :VFL语句中用到的控件

  2)创建一个字典(内部包含VFL语句中用到的控件)的快捷宏定义

   NSDictionaryOfVariableBindings(...)

   代码:NSDictionaryOfVariableBindings(v1, v2, v3) 和 [NSDictionary dictionaryWithObjectsAndKeys:v1, @"v1", v2, @"v2", v3, @"v3", nil] 是等价的

  • 代码:
    UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
// 不要将AutoresizingMask转为Autolayout的约束
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView]; UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
// 不要将AutoresizingMask转为Autolayout的约束
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView]; // 间距
NSNumber *margin = @; // 添加水平方向的约束
NSString *vfl = @"H:|-margin-[blueView]-margin-[redView(==blueView)]-margin-|";
NSDictionary *views = NSDictionaryOfVariableBindings(blueView, redView);
NSDictionary *mertrics = NSDictionaryOfVariableBindings(margin);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:vfl options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:mertrics views:views];
[self.view addConstraints:constraints]; // 添加竖直方向的间距
NSNumber *height = @;
NSString *vfl2 = @"V:[blueView(height)]-margin-|";
NSDictionary *mertrics2 = NSDictionaryOfVariableBindings(margin, height);
NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:vfl2 options:kNilOptions metrics:mertrics2 views:views];
[self.view addConstraints:constraints2];

  效果图如下:

  

  VFL存在一定的缺陷,它只能满足大部分的约束,不能满足存在倍数关系的约束!

【原】iOS学习之苹果原生代码实现Autolayout和VFL语言的更多相关文章

  1. 【原】iOS学习之苹果开发者账号的相关操作

    1.苹果开发者账号分类 按价格分类 免费 ① 个人申请账号 仅可以用于真机调试 ② 院校账号 仅可以用于真机调试 通过苹果认证的高校可以使用 99$ ① 个人账号 ② 企业(公司)账号 申请所需的条件 ...

  2. IOS学习笔记—苹果推送机制APNs

    转自:唐韧_Ryan http://blog.csdn.net/ryantang03/article/details/8482259 推送是解决轮询所造成的流量消耗和 电量消耗的一个比较好的解决方案, ...

  3. IOS开发之 ---- 苹果系统代码汉字转拼音

    NSString *hanziText = @"我是中国人";   if ([hanziText length]) {       NSMutableString *ms = [[ ...

  4. iOS: 学习笔记实例, 用代码控制视图创建与切换

    1. 创建iOS, Single View Application.2. 修改YYViewController.m // // YYViewController.m // DynamicViewDem ...

  5. IOS学习之路(代码实现自动布局)

    1.将一个试图放置在其父视图的中央位置,使用限制条件. 2.创建两个限制条件:一个是将目标视图的 center.x 位置排列在其父视图的 center.x 位置,并且另外一个是将目标视图的 cente ...

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

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

  7. 原 iOS深入学习(Block全面分析)http://my.oschina.net/leejan97/blog/268536

    原 iOS深入学习(Block全面分析) 发表于1年前(2014-05-24 16:45)   阅读(26949) | 评论(14) 39人收藏此文章, 我要收藏 赞21 12月12日北京OSC源创会 ...

  8. 【原】iOS学习47之第三方-FMDB

    将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...

  9. Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)

    在前面的文章中介绍的了如何使用Cordova进行跨平台应用的开发,使用Cordova的话基本上就不需要在写系统原生代码了,只要通过编写html页面和js方法即可. 但在有些特殊情况下,还是是需要htm ...

随机推荐

  1. iOS动态部署之RSA加密传输Patch补丁

    概要:这一篇博客主要说明下iOS客户端动态部署方案中,patch(补丁)是如何比较安全的加载到客户端中. 在整个过程中,需要使用RSA来加密(你可以选择其它的非对称加密算法),MD5来做校验(同样,你 ...

  2. 读取图像,LUT以及计算耗时

    使用LUT(lookup table)检索表的方法,提高color reduce时对像素读取的速度. 实现对Mat对象中数据的读取,并计算color reduce的速度. 方法一:使用Mat的ptr( ...

  3. Java性能调优之:idea变慢解决

    今天搬砖的时候遇到一个问题,idea总是卡死,完全无法愉快的玩耍.幸好机器是Linux系统的.于是通过以下方式解决了问题: 通过top命令,查看系统运行状态发现4个CPU中有1个CPU用户占用率为10 ...

  4. django rest framework 再撸体验

    曾经了解过. 放在一边,嫌麻烦. 如今身为leader,站在团队沟通的角度看看,还不错. 有几个优点: 1. api一览表 2. api web预览界面(类似.net的webservice预览界面), ...

  5. 转:IOC框架

    CSND上看了王泽滨的博客关于IOC的,觉得说的很透彻,地址为:http://blog.csdn.net/wanghao72214/article/details/3969594 1 IoC理论的背景 ...

  6. css-关于位置

    当你设置一个你想要相对的模块为relative 你这个模块为absolute 则你的这个absolute会相对relative的那个模块进行移动.

  7. ztree-demo 2

    <!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - Async</TITLE> <m ...

  8. axis2 调用webservice

    maven配置:主要引用包及plugins <properties> <axis2.version>1.6.1</axis2.version> </prope ...

  9. [原创]Windows Server 2003 物理机转换为VMware虚拟机出现VSS错误的处理

    一台Windows Server 2003 物理机需要转换为VMware虚拟机,工具为Vmware vCenter Converter Standalone 6.0,转换开始就出现错误“FAILED: ...

  10. 在Nodejs中如何调用C#的代码

    最近需要在Nodejs中用到C#的代码,从网上了解到可以采用Edgejs来实现Nodejs与C#的代码交互, 直接复制网上的代码运行总是出各种错,填了不少坑,现在把自己的案例代码大致整理一下,方便以后 ...