autoLayout-自动布局


#import "ViewController.h"


@interface ViewController ()


@end

/**

 *  

 1.frame 是以自身的尺寸来确定自身的位置

 2.autoLayout是参照视图的位置来定义自己的位置 (相对布局 通过约束视图与视图之间的关系来分配屏幕上的位置)

 3.VFL(visual Format Language 视觉格式化语言)通过添加字符串,来约束视图和视图之间的关系;使用autoLayout 必须把translatesAutoresizingMaskIntoConstraints禁用才可以使用相对布局是找一个参照物 拿参照物当做基础,设置他和参照物的相对距离 来设置自己的位置

 

 4.VFL需要有横向,竖向两个方向的约束

 H:横向

 | 表示他的父视图

 [textField] 表示是一个视图

 -50- 表示后面视图 与前面视图的距离 (后面视图是textField,前面视图是他的父视图)

 [textField(>=200)] 要约束视图的宽  (>=200)允许最小的宽度是200  如果是竖向  就是允许最小的高度

 

 @"H:|-50-[textField(>=200)]-50-|"

 

 距离坐边原点距离50   右边边界距离50    允许视图的最小宽度是200

 

 

 V: 竖向

 

 5.使用auotLayout适配的时候 以最小尺寸为基准

 

 

 

 //    VFL  横向 竖向布局

 //    @"H:" 设置横向布局

 //    @"V:" 设置竖向布局

 

 //    设置横向布局 距离参照视图的左侧边距

 //    @"H:|-20-"

 //    @"H:[view]-20-"

 

 //    @"H:|-20-[view(200)]" view的宽  永远是200

 //    @"H:|-20-[view(otherView)]" view的宽  otherView的宽相同

 //    @"H:|-20-[view(>=200)]" 设置横向布局 距离参照视图的左侧边距 设置view横向的尺寸 不能低于200

 

 //    @"H:|-20-[view(>=200)]-20-|" 设置横向布局 距离参照视图的左侧边距 设置view横向的尺寸 不能低于200 设置右侧与参照视图之间的间距

 

 

 6.视图使用属性的时候绑定key ,需要绑定他真实的名字_titleName

 

 self.titleName = _titleName

 */

@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

//    [self demo1];

    [self demo5];

}


//一个视图

/*- (void)demo1

{

    

    UIView *views = [[UIView alloc]init];

    views.translatesAutoresizingMaskIntoConstraints = NO;

    

    views.backgroundColor = [UIColor orangeColor];

    [self.view addSubview:views];

    

    //VFL 横向竖向布局

    //@"H:"设置横向布局;

    //@H:|-20- 设置距离父视图左侧的边距

    //@H:|-20-[views(>=200)] 设置横向布局,距离父视图左侧的边距 设置views的尺寸不能低于200

    //@"H:|-20-[views(>=200)-20-|]" 设置横向布局,距离父视图左侧的边距 设置views的尺寸不能低于200;设置右侧距离父视图左侧的边距

    // 竖向布局@"V:|-40-[views(>=400)-20-|]"

    //使用VFL需要把视图的对象与他的名字(字符串绑定起来)

    NSDictionary *viewsDic = NSDictionaryOfVariableBindings(views);

    

    //self.view views 添加约束;NSLayoutConstraint添加具体约束的 一个类

    //format:VFL;

    //opts:同意按照某个方向去布局;

    //metrics:绑定的参数;

    //views绑定的视图参数

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[views(>=200)]-20-|" options:0 metrics:nil views:viewsDic]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[views(>=200)]-20-|" options:0 metrics:nil views:viewsDic]];

}*/


- (void)demo2

{

    UIView *views = [[UIView alloc]init];

    views.translatesAutoresizingMaskIntoConstraints = NO;

    views.backgroundColor = [UIColor orangeColor];

    [self.view addSubview:views];

    

    UIView *views1 = [[UIView alloc]init];

    views1.translatesAutoresizingMaskIntoConstraints = NO;

    views1.backgroundColor = [UIColor redColor];

    [self.view addSubview:views1];

    

    NSDictionary *viewDic = NSDictionaryOfVariableBindings(views,views1);

    

    //orangeColor视图的约束

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[views(>=200)]-20-|" options:0 metrics:nil views:viewDic]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[views(50)]-10-[views1]" options:0 metrics:nil views:viewDic]];

    

    

    //red视图的约束

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[views1(>=200)]-20-|" options:0 metrics:nil views:viewDic]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[views]-10-[views1(50)]" options:0 metrics:nil views:viewDic]];

    

}



- (void)demo3

{

    UIView *views = [[UIView alloc]init];

    views.translatesAutoresizingMaskIntoConstraints = NO;

    views.backgroundColor = [UIColor orangeColor];

    [self.view addSubview:views];

    

    UIView *views1 = [[UIView alloc]init];

    views1.translatesAutoresizingMaskIntoConstraints = NO;

    views1.backgroundColor = [UIColor redColor];

    [self.view addSubview:views1];

    

    NSDictionary *viewDic = NSDictionaryOfVariableBindings(views,views1);

    

    //orangeColor视图的约束

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[views(>=200)]-20-|" options:0 metrics:nil views:viewDic]];

    

    //两个视图都在同一个父视图上,且视图之间又有联系   两个视图竖向约束

//    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[views(50)]-10-[views1(50)]" options:0 metrics:nil views:viewDic]];

    

    

    //两个视图高度都是50

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[views(50)]-10-[views1(views)]" options:0 metrics:nil views:viewDic]];

    

    //red视图的约束

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[views1(>=200)]-20-|" options:0 metrics:nil views:viewDic]];

//    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[views]-10-[views1(50)]" options:0 metrics:nil views:viewDic]];

    

}


- (void)demo4

{

    

    NSArray *colorList = @[[UIColor redColor],[UIColor yellowColor]];

    for (int i = 0; i < 2; i ++) {

        

        UIView *views = [[UIView alloc]init];

        views.translatesAutoresizingMaskIntoConstraints = NO;

        views.backgroundColor = colorList[i];

        [self.view addSubview:views];

        views.tag = 10+i;

        

    }

    

    UIView *redView = [self.view viewWithTag:10];

    UIView *yellowView = [self.view viewWithTag:11];

    

    NSDictionary *viewsDic = NSDictionaryOfVariableBindings(redView,yellowView);

    //两个视图横向的约束

    NSArray *constains = @[@"H:|-20-[redView(>=200)]-20-|",@"H:|-20-[yellowView(>=200)]-20-|"];

    for (NSString *VFL in constains) {

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:VFL options:0 metrics:nil views:viewsDic]];

    }

    

    //两个视图竖向的视图

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[redView(50)]-10-[yellowView(redView)]" options:0 metrics:nil views:viewsDic]];

    

}


- (void)demo5

{

    NSArray *colorList = @[[UIColor redColor],[UIColor yellowColor],[UIColor blueColor]];

    for (int i = 0; i < colorList.count; i++) {

        UIView *views = [[UIView alloc]init];

        views.translatesAutoresizingMaskIntoConstraints = NO;

        views.backgroundColor = colorList[i];

        views.tag = 20+i;

        [self.view addSubview:views];

        

    }

    

    UIView *redView = [self.view viewWithTag:20];

    UIView *yellowView = [self.view viewWithTag:21];

    UIView *blueView = [self.view viewWithTag:22];

    

    NSDictionary *viewsDic = NSDictionaryOfVariableBindings(redView,yellowView,blueView);

    

    NSArray *Hconstaints = @[@"H:|-20-[redView(>=200)]-20-|",@"H:|-20-[yellowView(>=100)]-10-[blueView(yellowView)]-20-|"];

    

    //红色视图与黄色视图纵向的关系,红色视图与蓝色视图纵向的关系

    NSArray *Vconstaints = @[@"V:|-40-[redView(50)]-10-[yellowView(redView)]",@"V:|-40-[redView(50)]-10-[blueView(redView)]"];

    

    for (int i = 0; i < Vconstaints.count; i++) {

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:Hconstaints[i] options:0 metrics:nil views:viewsDic]];

        

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:Vconstaints[i] options:0 metrics:nil views:viewsDic]];

    }

    

    

}





- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值