IOS-Frameworks-UIKit-NSLayoutConstraint 自动布局注意事项

//自动布局注意事项:

-(void)layoutTestFunc

{

    

   UIView * view0 = [[UIViewalloc]init];

    

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

    view0.translatesAutoresizingMaskIntoConstraints =NO;

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

    [self.viewaddSubview: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 = [NSLayoutConstraintconstraintWithItem:view0 attribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:self.viewattribute:NSLayoutAttributeWidthmultiplier:0.0constant:9];

   NSLog(@"%@",con0);

    

    //secondItemnil时,secondAttribute应为NSLayoutAttributeNotAnAttribute

    NSLayoutConstraint * con1 = [NSLayoutConstraintconstraintWithItem:view0 attribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:0.0constant:9];

   

    

    [self.viewaddConstraint:con1];

    

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

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

    

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

    NSLog(@"%@",view0.constraints);

    

    

   UIView * view1 = [[UIViewalloc]init];

   UIView * view2 = [[UIViewalloc]init];

   UIView * view3 = [[UIViewalloc]init];

    view1.translatesAutoresizingMaskIntoConstraints =NO;

    view2.translatesAutoresizingMaskIntoConstraints =NO;

    view3.translatesAutoresizingMaskIntoConstraints =NO;

    

    [view2addSubview:view3];

    [view1addSubview:view2];

    [self.viewaddSubview:view1];

    

    NSLayoutConstraint * con2 = [NSLayoutConstraintconstraintWithItem:view2 attribute:NSLayoutAttributeLeadingrelatedBy:NSLayoutRelationEqualtoItem:view3 attribute:NSLayoutAttributeLeadingmultiplier:1.0constant:39];

    

    [self.viewaddConstraint:con2];

    

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

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

    

    //空,虽然约束是关于view2,与view3view2view3最近的父视图是view1,但是下面全为空。

    NSLog(@"%@",view1.constraints);

   //

    NSLog(@"%@",view2.constraints);

   //

    NSLog(@"%@",view3.constraints);

}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014222687/article/details/47360617
文章标签: cocoa 布局 iOS
个人分类: IOS
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

IOS-Frameworks-UIKit-NSLayoutConstraint 自动布局注意事项

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭