iOS AutoLayout自动布局中级开发教程(8)-VisualFormat可视化格式语言创建约束

如何使用visualFormat语言纯代码写约束呢?经过本人的学习,写出来与大家分享一下:

使用storyboard可以完成我们的大部分的布局需求,然而storyboard不是万能的,在对于后续的可维护性较高的工程中最好使用手写代码,这就需要我们掌握纯手写约束这项基本能力;

我们在苹果的doc文档里面可以看到一些关于VisualFormat的介绍,但这仅仅是个介绍,我们在实际的应用过程中需要更多的知识:



首先看一下,手写约束的效果图:



图中的两个按钮要求:

1.两个Button的宽度相等=100

2.两个Button的顶部对齐

3._button1的左边距离父视图为默认的间隔(20)

4.button2左边距离_button1为20,这个20约束的优先级是 750;button2右边距离父视图距离为0

首先,看一下使用VisualFormat实现该效果的代码:

注意:为了更全面的介绍VisualFormat,写法格式可能会有多种!!!

.h

  1. //  
  2. //  ViewController.h  
  3. //  AutoLayout2  
  4. //  
  5. //  Created by yb on 15/2/9.  
  6. //  Copyright (c) 2015年 http://blog.csdn.net/yangbingbinga. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface ViewController : UIViewController  
  12.   
  13.   
  14. @end  

.m

  1. //  
  2. //  ViewController.m  
  3. //  AutoLayout2  
  4. //  
  5. //  Created by yb on 15/2/9.  
  6. //  Copyright (c) 2015年 http://blog.csdn.net/yangbingbinga. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12. {  
  13.     UIButton *button2//对应的visualFormat名称是  @"[button2]"  
  14. }  
  15. @property(nonatomic,strong)UIButton *button1;//对应的visualFormat名称是;@"[_button1]",有下划线  
  16. @end  
  17.   
  18. @implementation ViewController  
  19.   
  20. - (void)viewDidLoad  
  21. {  
  22.     [super viewDidLoad];  
  23.     _button1=[UIButton buttonWithType:UIButtonTypeCustom];  
  24.     _button1.backgroundColor=[UIColor blueColor];  
  25.     _button1.translatesAutoresizingMaskIntoConstraints=NO;  
  26.     button2=[UIButton buttonWithType:UIButtonTypeCustom];  
  27.     button2.backgroundColor=[UIColor redColor];  
  28.     button2.translatesAutoresizingMaskIntoConstraints=NO;  
  29.     [self.view addSubview:_button1];  
  30.     [self.view addSubview:button2];  
  31.     NSArray *hCons=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_button1(100)]-50@750-[button2(==_button1)]|" options:NSLayoutFormatAlignAllTop metrics:0 views:NSDictionaryOfVariableBindings(_button1,button2) ];  
  32.       
  33.     [self.view addConstraints:hCons];  
  34.       
  35.     NSArray *vCons=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(100)-[_button1(100)]" options:0 metrics:0 views:@{@"_button1":_button1,@"button2":button2}];  
  36.     [self.view addConstraints:vCons];  
  37.       
  38.     NSArray *vCons1=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button2(==50)]" options:0 metrics:0 views:@{@"_button1":_button1,@"button2":button2}];  
  39.     [self.view addConstraints:vCons1];  
  40.       
  41. }  
  42. @end  
说明:

1.

  1. _button1=[UIButton buttonWithType:UIButtonTypeCustom];  

要使用VisualFormat一定要提前分配好内存,否则会在使用 constraintsWithVisualFormat的_view参数,会导致崩溃
2.
_button1.translatesAutoresizingMaskIntoConstraints=NO;
使用AutoLayout和默认的autoSizing有冲突,默认 是YES,使用代码 写约束,一定要设置为NO,关闭自动调整,AutoResizing
3.
  1. NSArray *hCons=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_button1(100)]-50@750-[button2(==_button1)]|" options:NSLayoutFormatAlignAllTop metrics:0 views:NSDictionaryOfVariableBindings(_button1,button2) ];  
  1. 使用constraintsWithVisualFormat方法创建一个约束数组,默认只有一个元素  
  1. 参数说明:(1)@"H:|-[_button1(100)]-50@750-[button2(==_button1)]|"  
  1. H代表水平方向上,_button1距离 父视图 为 - (子视图与父视图之间默认间隔为20),[_button(100)] 代表,宽度为为100也可以写成[_button(>=100)];  
  1. 50@750,代表 _button1和button2之间的间隔是 50优先级为 750(默认的约束的优先级是1000,所以该约束可能不会使用);[button2(==_button1)]代表 button2的宽度等于_button1的宽度,也可以写成[button2(_button1)]  
 
  
 (2)options:NSLayoutFormatAlignAllTop 
  
对齐约束,比如上面的约束是对齐约束 ,顶部对齐!
其他对齐方式:
  1. NSLayoutFormatAlignAllLeft      
  1. NSLayoutFormatAlignAllRight     
  1. NSLayoutFormatAlignAllTop       
  2. NSLayoutFormatAlignAllBottom  
  3. NSLayoutFormatAlignAllLeading      
  1. NSLayoutFormatAlignAllTrailing   
  1. NSLayoutFormatAlignAllCenterX      
  1. NSLayoutFormatAlignAllCenterY     
  1. NSLayoutFormatAlignAllBaseline      
  1. NSLayoutFormatAlignAllLastBaseline  
这是一个便利的方式,在添加水平约束的时候,可以添加 垂直方向的;垂直方向上 同理
 
 

(3)

  1. metrics:0   
  1. 该参数是一个字典:  
  1. 例如  
  1. NSDictionary *metrics=@{@"space":@100}也就是,例如在如下的VisualFormat字符串中使用  @"H:[_button1]-space-[button2]"  
  1. 这样,space这个间隔就可以替换成我们想要的约束了!  
  1. (4)绑定变量  
  1. views:NSDictionaryOfVariableBindings(_button1,button2)] 效果等价于:  
  1. NSDictionary *dict=@{@"_button1":_button1,@"button2":button};  
  1. 注意对应的 _button对象和 button不能为空!  
  1. 系统根据你给的字典,来找到约束对应的对象!  
  1. 技巧:  
  1. 我们也可以使用NSString *visualString=[NSString stringWithFormat:@"H:|[_button1(%g),100.0];来添加 变化的宽度或者其他约束,效果和  metrics类似;对于类似于:@"H:|-[_button1(100)]-50@750-[button2(==_button1)]|"的可视化格式字符串,我将在下一篇文章中详细介绍  
  1. 更多autolayout文章,原文地址http://blog.csdn.net/yangbingbinga  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值