自动布局 01 注意事项

自动布局 01 注意事项,第1张

概述//自动布局注意事项: -(void)layoutTestFunc {          UIView * view0 = [[UIView alloc]init];          //注意事项1:设置translatesAutoresizingMaskIntoConstraints为NO;     view0.translatesAutoresizingMaskIntoConstraints

//自动布局注意事项:

-(voID)layoutTestFunc

{

    

    UIVIEw * vIEw0 = [[UIVIEw alloc]init];

    

    //注意事项1:设置translatesautoresizingMaskIntoConstraintsNO;

    vIEw0.translatesautoresizingMaskIntoConstraints = NO;

    //注意事项2:两个vIEw在同一个hIErarchy中才有可能添加相关约束。

    [self.vIEw addSubvIEw:vIEw0];

    

    //注意事项3:firstAttribute不为宽或者高时,multipler不能为0

    /*

    NSLayoutConstraint * con = [NSLayoutConstraint constraintWithItem:vIEw0 attribute:NSLayoutAttributeleft relatedBy:NSLayoutRelationEqual toItem:self.vIEw attribute:NSLayoutAttributeleft multiplIEr:0.0 constant:9];

    //[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplIEr:constant:]: A multiplIEr of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specifIEd in pairs'

    */

    

    //注意事项4:同上,当firstAttribute不为宽或者高时,secondItem不能为nil

    //NSLayoutConstraint * con = [NSLayoutConstraint constraintWithItem:vIEw0 attribute:NSLayoutAttributeleft relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeleft multiplIEr:1.0 constant:9];


    //当两个Attribute全是宽或高时multiplIEr才可以为0

    NSLayoutConstraint * con0 = [NSLayoutConstraint constraintWithItem:vIEw0 attribute:NSLayoutAttributeWIDth relatedBy:NSLayoutRelationEqual toItem:self.vIEw attribute:NSLayoutAttributeWIDth multiplIEr:0.0 constant:9];

    NSLog(@"%@",con0);

    

    //secondItemnil时,secondAttribute应为NSLayoutAttributeNotAnAttribute

    NSLayoutConstraint * con1 = [NSLayoutConstraint constraintWithItem:vIEw0 attribute:NSLayoutAttributeWIDth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplIEr:0.0 constant:9];

   

    

    [self.vIEw addConstraint:con1];

    

    //包括:"<NSLayoutConstraint:0x7fb5a84675e0 H:[UIVIEw:0x7fb5a86528c0(9)]>"

    NSLog(@"%@",self.vIEw.constraints);

    

    //空,注意虽然con理论上只涉及vIEw0,但是vIEw0constraints并不包含约束con1

);

    

    

    UIVIEw * vIEw1 = [[UIVIEw alloc]init];

    UIVIEw * vIEw2 = [[UIVIEw alloc]init];

    UIVIEw * vIEw3 = [[UIVIEw alloc]init];

    vIEw1.translatesautoresizingMaskIntoConstraints = NO;

    vIEw2.translatesautoresizingMaskIntoConstraints = NO;

    vIEw3.translatesautoresizingMaskIntoConstraints = NO;

    

    [vIEw2 addSubvIEw:vIEw3];

    [vIEw1 addSubvIEw:vIEw2];

    [self.vIEw addSubvIEw:vIEw1];

    

    NSLayoutConstraint * con2 = [NSLayoutConstraint constraintWithItem:vIEw2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:vIEw3 attribute:NSLayoutAttributeLeading multiplIEr:1.0 constant:39];

    

    [self.vIEw addConstraint:con2];

    

self.vIEw.constraints);

    //包括"<NSLayoutConstraint:0x7f82e0d2b090 UIVIEw:0x7f82e0d2ab50.leading == UIVIEw:0x7f82e0d2ae80.leading + 39>"

    

    //空,虽然约束是关于vIEw2,与vIEw3vIEw2vIEw3最近的父视图是vIEw1,但是下面全为空。

);

    //

);

    //);

}

总结

以上是内存溢出为你收集整理的自动布局 01 注意事项全部内容,希望文章能够帮你解决自动布局 01 注意事项所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1051662.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存