VFL的使用

由于项目中一直没使用XIB,storyboard之类的,再加上现在屏幕那么多,适配起来真不是一件轻松的事情,无奈,只能使用VFL。根据这个VFL,说说自己使用过程中的一些看法。(H 和 V老是弄混,哎)

1、addSubview要在addConstraint之前。例如:

_detailInfoLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _detailInfoLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self.contentView addSubview:_detailInfoLabel];
        NSArray *constraintsForItemValueH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_detailInfoLabel]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_detailInfoLabel)];
        NSArray *constraintsForItemValueV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_detailInfoLabel]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_detailInfoLabel)];
        [self.contentView addConstraints:constraintsForItemValueH];
        [self.contentView addConstraints:constraintsForItemValueV];

如果第三行代码是放在最后,则会遇到如下的错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
Unable to interpret '|' character, because the related view doesn't have a superview 
H:|-0-[_detailInfoLabel]-0-| 
                        ^'

错误信息很清楚,你都还没add到某个view上呢,我怎么去设置约束。

2、约束该怎么去写,当然主要是那个format。举个横向约束的例子。

1、首先先写一个

H:

2、左边是和谁约束?如果是和父视图,那就直接加一丨,如下:

H:|

和其它的视图呢?简单,直接 [视图名],加入另外一个视图叫testLabel,如下:

<pre name="code" class="objc">H:[testLabel]-5-

 

如果和左边没有任何约束呢?那这一步直接跳到第四步,保持不变

3、距离左边多远呢?那就是-width-,比如距离父视图左边5个像素,则如下:

<pre name="code" class="objc">H:|-5-

 

其它视图的就如下:

V:[testLabel]-5-

4、加上当前要约束的视图[当前要约束的视图],如下:

H:|-5-[currentView]

5、后面的就和左边的写法是一样的,例如距离左边5个像素,结果如下:

H:|-5-[currentView]-5-|

6、OK,一个距离左右父视图各5个像素的约束就写好了,当然,如果你想设置它的宽度,那简单,如果是常量,就直接在当前视图后面加个括号里面放入常量值,比如,距离父视图左边5个像素,宽度100个像素:

H:|-5-[currentView(100)]

如果是参数呢?那就赋值咯,比如宽度变量是 width

H:|-5-[currentView(==width)]

width在什么地方赋值呢?下面再说。

7、

/* Create an array of constraints using an ASCII art-like visual format string.
 */
+ (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;
参数就是metrics,一个字典,key就是你的变量名,比如上一步的width,这里面你就可以给个参数,例如宽度100:

@{@"width":@(100)}

8、最后一个参数,就是views,这个是由如下这个方法得到的,

/* This macro is a helper for making view dictionaries for +constraintsWithVisualFormat:options:metrics:views:.  
 NSDictionaryOfVariableBindings(v1, v2, v3) is equivalent to [NSDictionary dictionaryWithObjectsAndKeys:v1, @"v1", v2, @"v2", v3, @"v3", nil];
 */
#define NSDictionaryOfVariableBindings(...) _NSDictionaryOfVariableBindings(@"" # __VA_ARGS__, __VA_ARGS__, nil)

将你前面几步里面涉及到的视图变量名全写进去。切记:给的这个字典必须和你写的format里的视图相匹配,否则,给出一些乱七八糟的错误,很纠结。

9、OK,这个横向的约束就写完了,后面你再写纵向的约束。

10、最后,别忘记了,设置一下视图的

/* by default, the autoresizing mask on a view gives rise to constraints that fully determine the view's position.  Any constraints you set on the view are likely to conflict with autoresizing constraints, so you must turn off this property first. IB will turn it off for you.
 */
- (BOOL)translatesAutoresizingMaskIntoConstraints NS_AVAILABLE_IOS(6_0); // Default YES

这个属性为NO。

不过,能使用XIB或者storyboard就使用他们咯。VFL代码量真心大。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值