AutoLayout

2020-10-28  本文已影响0人  我不白先生
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *myView1;
@property (weak, nonatomic) IBOutlet UIView *myView2;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //关闭了 myView1 将 autoresizing 自动转换成 约束对象的 功能
    self.myView1.translatesAutoresizingMaskIntoConstraints =NO;
    //关闭了 myView2 将 autoresizing 自动转换成 约束对象的 功能
    self.myView2.translatesAutoresizingMaskIntoConstraints =NO;
    //给 view 创建约束对象 添加到 self.view
    /*************myView1的约束**************/
    //1.创建左边距约束 myView1.left = self.view.left * 1 + 20
    NSLayoutConstraint *c1 = [NSLayoutConstraint constraintWithItem:self.myView1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:20];
    //2.创建上边距约束
    NSLayoutConstraint *c2 = [NSLayoutConstraint constraintWithItem:self.myView1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:20];
    //3.创建高边距约束
        NSLayoutConstraint *c3 = [NSLayoutConstraint constraintWithItem:self.myView1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40];
    //4.创建宽度约束
    NSLayoutConstraint *c4 = [NSLayoutConstraint constraintWithItem:self.myView1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.myView2 attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    //将创建好的约束条件 添加到 父视图中
    [self.view addConstraint:c1];
     [self.view addConstraint:c2];
     [self.view addConstraint:c3];
     [self.view addConstraint:c4];
     /*************myView2的约束**************/
    //1.创建左边约束
    NSLayoutConstraint *c5 = [NSLayoutConstraint constraintWithItem:self.myView2 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.myView1 attribute:NSLayoutAttributeRight multiplier:1 constant:10];
    //2.创建顶部约束
     NSLayoutConstraint *c6= [NSLayoutConstraint constraintWithItem:self.myView2 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.myView1 attribute:NSLayoutAttributeCenterY  multiplier:1 constant:0];
    //3.创建高度约束
    NSLayoutConstraint *c7= [NSLayoutConstraint constraintWithItem:self.myView2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.myView1 attribute:NSLayoutAttributeHeight  multiplier:1 constant:0];
    //3.创建右边距约束
    NSLayoutConstraint *c8= [NSLayoutConstraint constraintWithItem:self.myView2 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:-20];
    [self.view addConstraint:c5];
    [self.view addConstraint:c6];
    [self.view addConstraint:c7];
    [self.view addConstraint:c8];
}
@end

练习

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *myView1;
@property (weak, nonatomic) IBOutlet UIView *myView2;
@property (weak, nonatomic) IBOutlet UIView *myView3;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.myView1.translatesAutoresizingMaskIntoConstraints = NO;
    self.myView2.translatesAutoresizingMaskIntoConstraints = NO;
    self.myView3.translatesAutoresizingMaskIntoConstraints = NO;
    //myView1
    //距离视图左边20点
    NSLayoutConstraint *c1 = [NSLayoutConstraint constraintWithItem:self.myView1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:20];
    //距离视图右边20点
    NSLayoutConstraint *c2 = [NSLayoutConstraint constraintWithItem:self.myView1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:-20];
    //高边距40点
    NSLayoutConstraint *c3 = [NSLayoutConstraint constraintWithItem:self.myView1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40];
    //创建宽度约束
    NSLayoutConstraint *c4 = [NSLayoutConstraint constraintWithItem:self.myView1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:-40];
    [self.view addConstraint:c1];
    [self.view addConstraint:c2];
    [self.view addConstraint:c3];
    [self.view addConstraint:c4];
    //myView2
    //创建宽度约束
     NSLayoutConstraint *c5 = [NSLayoutConstraint constraintWithItem:self.myView2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.myView1 attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
    //创建高度约束
    NSLayoutConstraint *c6 = [NSLayoutConstraint constraintWithItem:self.myView2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.myView2 attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    NSLayoutConstraint *c7= [NSLayoutConstraint constraintWithItem:self.myView2 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    NSLayoutConstraint *c8= [NSLayoutConstraint constraintWithItem:self.myView2 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:c5];
    [self.view addConstraint:c6];
    [self.view addConstraint:c7];
    [self.view addConstraint:c8];
     //myView3
    //创建高度约束
    NSLayoutConstraint *c9 = [NSLayoutConstraint constraintWithItem:self.myView3 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.myView1 attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
    NSLayoutConstraint *c10 = [NSLayoutConstraint constraintWithItem:self.myView3 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.myView1 attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    NSLayoutConstraint *c11 = [NSLayoutConstraint constraintWithItem:self.myView3 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:20];
    NSLayoutConstraint *c12 = [NSLayoutConstraint constraintWithItem:self.myView3 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:-20];
    [self.view addConstraints:@[c9,c10,c11,c12]];
}
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UIView *myView1;
@property(nonatomic,strong)UIView *myView2;
@end

@implementation ViewController
-(UIView *)myView1{
    if(!_myView1){
        _myView1 = [[UIView alloc]init];
        _myView1.backgroundColor =[UIColor redColor];
        _myView1.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:_myView1];
     
    }
    return _myView1;
}
-(UIView *)myView2{
    if(!_myView2){
        _myView2 = [[UIView alloc]init];
        _myView2.backgroundColor =[UIColor redColor];
        _myView2.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:_myView2];
    }
    return _myView2;
}
-(void)vfl1{
    //添加约束
    //拼写约束所需的 VFL 字符串内容
    NSString *hVFL = @"|-20-[view1]-10-[view2(view1)]-20-|";
    NSArray *cs1 = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:@{@"view1":self.myView1,@"view2":self.myView2}];
    //将约束数组添加到 父视图中
    [self.view addConstraints:cs1];
    NSString *vVFL = @"V:|-20-[view1(40)]";
    NSArray *cs2 = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options:NSLayoutFormatAlignmentMask metrics:nil views:@{@"view1":self.myView1}];
    [self.view addConstraints:cs2];
}
-(void)vfl2{
    UIView *myView3 = [[UIView alloc]init];
    myView3.backgroundColor = [UIColor orangeColor];
    myView3.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:myView3];
    UIView *myView4 = [[UIView alloc]init];
    myView4.backgroundColor = [UIColor blueColor];
    myView4.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:myView4];
    
    //NSDictionaryOfVariableBindings 传入的是 value对象,会根据传入的对象名称自动生成同名的key
    NSDictionary *viewsDic = NSDictionaryOfVariableBindings(myView3,myView4);
    //创建对照表
    NSDictionary *metrics = @{@"left":@20,@"right":@20,@"space":@10,@"height":@40,};
    //添加约束
    //拼写约束所需的 VFL 字符串内容
    NSString *hVFL = @"|-left-[myView3]-space-[myView4(myView3)]-right-|";
    NSArray *cs1 = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:viewsDic];
    //将约束数组添加到 父视图中
    [self.view addConstraints:cs1];
    
    NSString *vVFL = @"V:|-left-[myView3(height)]";
    NSArray *cs2 = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options:NSLayoutFormatAlignmentMask metrics:metrics views:viewsDic];
    [self.view addConstraints:cs2];
}
- (void)viewDidLoad {
    [super viewDidLoad];
 //[self vfl1];
    [self vfl2];
}
@end
上一篇下一篇

猜你喜欢

热点阅读