autolayout 设置高度限制和宽度限制

//DEMO LOGO
UIImageView *demoLogo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"DEMO LOGO.png"]];
[demoLogo setAlpha:0.88];
[self.view addSubview:demoLogo];
[demoLogo setTranslatesAutoresizingMaskIntoConstraints:NO];

//与superview的底部始终差距50个单位高度(不能小于)
NSLayoutConstraint *myConstraint;
myConstraint = [NSLayoutConstraint constraintWithItem:demoLogo attribute:NSLayoutAttributeBottom 
                relatedBy:NSLayoutRelationGreaterThanOrEqual 
                toItem:[demoLogo superview] attribute:NSLayoutAttributeBottom 
                multiplier:1.0f constant:-50.0f];
[self.view addConstraint:myConstraint];

//x轴对齐superview的x轴中心点
myConstraint = [NSLayoutConstraint constraintWithItem:demoLogo attribute:NSLayoutAttributeCenterX 
                relatedBy:NSLayoutRelationEqual 
                toItem:[demoLogo superview] attribute:NSLayoutAttributeCenterX 
                multiplier:1.0f constant:-0.0f];
[self.view addConstraint:myConstraint];

//宽度限制
myConstraint = [NSLayoutConstraint constraintWithItem:demoLogo attribute:NSLayoutAttributeWidth 
                relatedBy:NSLayoutRelationEqual 
                toItem:nil attribute:NSLayoutAttributeNotAnAttribute 
                multiplier:1.0f constant:210.0f];
[self.view addConstraint:myConstraint];

//高度限制
myConstraint = [NSLayoutConstraint constraintWithItem:demoLogo attribute:NSLayoutAttributeHeight 
                relatedBy:NSLayoutRelationEqual 
                toItem:nil attribute:NSLayoutAttributeNotAnAttribute 
                multiplier:1.0f constant:20.0f];
[self.view addConstraint:myConstraint];

你可能感兴趣的:(ios)