试写VFL语句及网上的一些资料

来源:互联网 发布:中海达v30数据怎么导出 编辑:程序博客网 时间:2024/06/05 10:14

试写了一些VFL语句:

(1)NSArray *constraints1=[NSLayoutConstraint constraintsWithVisualFormat:

@"H:|-40-[button]-40-|"
options:0
metrics:nil                                                       views:NSDictionaryOfVariableBindings(button)];

分析一下这个句子:H表示水平方向;|表示self.view也就是控制器的视图;40表示button距离self.view左右各40距离(也就是button的宽度为self.view.width-80);

(2)NSArray *constraints3=[NSLayoutConstraint constraintsWithVisualFormat:

@"H:[button1(==120)]-(-120)-[button]"
options:0
metrics:nil                  views:NSDictionaryOfVariableBindings(button1,button)];

分析一下这个句子:[button1(==120)]表示button1的宽度为120;button1距离button的左侧的距离为-120;需要注意views里面需要包含button

(3)NSArray *constraints3=[NSLayoutConstraint constraintsWithVisualFormat:

@"H:[button1(==height)]-(-height)-[button]"
options:0
metrics:@{@"height":@120}                  views:NSDictionaryOfVariableBindings(button1,button)];

对比(2),这里主要是转移了长度。在metrics里面添加了一个height为120的长度;

(4)与父视图居中对齐

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];


//以下地方为转载:

[cancelButton(72)]-12-[acceptButton(50)]
取消按钮宽72point,accept按钮宽50point,它们之间间距12point
[wideView(>=60@700)]
wideView宽度大于等于60point,该约束条件优先级为700(优先级最大值为1000,优先级越高的约束越先被满足)
V:[redBox][yellowBox(==redBox)]
竖直布局,先是一个redBox,其下方紧接一个宽度等于redBox宽度的yellowBox
H:|-[Find]-[FindNext]-[FindField(>=20)]-|
水平布局,Find距离父view左边缘默认间隔宽度,之后是FindNext距离Find间隔默认宽度;再之后是宽度不小于20的FindField,它和FindNext以及父view右边缘的间距都是默认宽度。(竖线’|‘ 表示superview的边缘)



容易出现的错误
因为涉及约束问题,因此约束模型下的所有可能出现的问题这里都会出现,具体来说包括两种:
Ambiguous Layout 布局不能确定
Unsatisfiable Constraints 无法满足约束
布局不能确定指的是给出的约束条件无法唯一确定一种布局,也即约束条件不足,无法得到唯一的布局结果。这种情况一般添加一些必要的约束或者调整优先级可以解决。无法满足约束的问题来源是有约束条件互相冲突,因此无法同时满足,需要删掉一些约束。两种错误在出现时均会导致布局的不稳定和错误,Ambiguous可以被容忍并且选择一种可行布局呈现在UI上,Unsatisfiable的话会无法得到UI布局并报错。

对于不能确定的布局,可以通过调试时暂停程序,在debugger中输入
po [[UIWindow keyWindow] _autolayoutTrace]
来检查是否存在Ambiguous Layout以及存在的位置,来帮助添加条件。另外还有一些检查方法,来查看view的约束和约束状态:

[view constraintsAffectingLayoutForOrientation/Axis: NSLayoutConstraintOrientationHorizontal/Vertical]
[view hasAmbiguousLayout]
[view exerciseAmbiguityInLayout]

布局动画
动画是UI体验的重要部分,更改布局以后的动画也非常关键。说到动画,Core Animation又立功了..自从CA出现以后,所有的动画效果都非常cheap,在auto layout中情况也和collection view里一样,很简单(可以参考WWDC 2012 Session笔记——219 Advanced Collection Views and Building Custom Layouts),只需要把layoutIfNeeded放到animation block中即可~

[UIView animateWithDuration:0.5 animations:^{
    [view layoutIfNeeded];
}];
 

如果对block不熟悉的话,可以看看我很早时候写的一篇block的文章。

- See more at: http://www.onevcat.com/2012/09/autoayout/#sthash.Kv4qjr2T.dpuf

0 0
原创粉丝点击