//自动布局注意事项:
-(void)layoutTestFunc
{
UIView * view0 = [[UIView alloc]init];
//注意事项1:设置translatesAutoresizingMaskIntoConstraints为NO;
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);
//当secondItem为nil时,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,但是view0的constraints并不包含约束con1;
NSLog(@"%@",view0.constraints);
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];
NSLog(@"%@",self.view.constraints);
//包括"<NSLayoutConstraint:0x7f82e0d2b090 UIView:0x7f82e0d2ab50.leading == UIView:0x7f82e0d2ae80.leading + 39>"
//空,虽然约束是关于view2,与view3,view2月view3最近的父视图是view1,但是下面全为空。
NSLog(@"%@",view1.constraints);
//空
NSLog(@"%@",view2.constraints);
//空
NSLog(@"%@",view3.constraints);
}