IOS学习之NSLayoutConstraint基础篇

IOS学习之NSLayoutConstraint基础篇_第1张图片

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //======创建第一个view======
    UIView *myView=[[UIView alloc]init];
    myView.backgroundColor=[UIColor blueColor];
    [self.view addSubview:myView];
    
    //view自身禁用autoResizing
    myView.translatesAutoresizingMaskIntoConstraints=NO;
    
    //创建约束
    //A=B * multiplier + constant
    //设置自身高度
    [myView addConstraint:[NSLayoutConstraint
                              constraintWithItem:myView
                              attribute:NSLayoutAttributeHeight
                              relatedBy:NSLayoutRelationEqual
                              toItem:nil
                              attribute:NSLayoutAttributeNotAnAttribute
                              multiplier:1
                              constant:50]];
    //距离左边40(父控件增加约束)
    [self.view addConstraint:[NSLayoutConstraint
                         constraintWithItem:myView
                         attribute:NSLayoutAttributeLeft
                         relatedBy:NSLayoutRelationEqual
                         toItem:self.view
                         attribute:NSLayoutAttributeLeft
                         multiplier:1
                         constant:40]];
    //距离右边40(父控件增加约束)
    [self.view addConstraint:[NSLayoutConstraint
                           constraintWithItem:myView
                           attribute:NSLayoutAttributeRight
                           relatedBy:NSLayoutRelationEqual
                           toItem:self.view
                           attribute:NSLayoutAttributeRight
                           multiplier:1
                           constant:-40]];
    //距离上边状态栏28(父控件增加约束)
    [self.view addConstraint:[NSLayoutConstraint
                           constraintWithItem:myView
                           attribute:NSLayoutAttributeTop
                           relatedBy:NSLayoutRelationEqual
                           toItem:self.topLayoutGuide
                           attribute:NSLayoutAttributeBottom
                           multiplier:1
                           constant:28]];
    
    //======创建第二个view======
    UIView *myView2=[[UIView alloc]init];
    myView2.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:myView2];
    
    //view自身禁用autoResizing
    myView2.translatesAutoresizingMaskIntoConstraints=NO;
    
    //创建约束
    //设置 第二个view高度=第一个view高度
    [self.view addConstraint:[NSLayoutConstraint
                           constraintWithItem:myView2
                           attribute:NSLayoutAttributeHeight
                           relatedBy:NSLayoutRelationEqual
                           toItem:myView
                           attribute:NSLayoutAttributeHeight
                           multiplier:1
                           constant:0]];

    //距离上边28
    [self.view addConstraint:[NSLayoutConstraint
                              constraintWithItem:myView2
                              attribute:NSLayoutAttributeTop
                              relatedBy:NSLayoutRelationEqual
                              toItem:myView
                              attribute:NSLayoutAttributeBottom
                              multiplier:1
                              constant:28]];
    //距离左边40
    [self.view addConstraint:[NSLayoutConstraint
                              constraintWithItem:myView2
                              attribute:NSLayoutAttributeLeft
                              relatedBy:NSLayoutRelationEqual
                              toItem:self.view
                              attribute:NSLayoutAttributeLeft
                              multiplier:1
                              constant:40]];
    
    //第二个view宽度=第一个view宽度 * 0.5
    [self.view addConstraint:[NSLayoutConstraint
                              constraintWithItem:myView2
                              attribute:NSLayoutAttributeWidth
                              relatedBy:NSLayoutRelationEqual
                              toItem:myView
                              attribute:NSLayoutAttributeWidth
                              multiplier:0.5
                              constant:0]];

}

@end


你可能感兴趣的:(IOS,Object-C)