AutoLayout自定义布局基础

来源:互联网 发布:复制信息打开手机淘宝 编辑:程序博客网 时间:2024/06/03 19:49

AutoLayout自定义布局基础代码如下:


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self demo4];

}

- (BOOL)prefersStatusBarHidden

{

    return YES;

}

//一个视图

- (void)demo1

{

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

    view.backgroundColor = [UIColor cyanColor];

    view.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:view];

    

//    VFL横向 竖向相布局

//    @"H:|-20-[view(>=200)]-20-|"

//    @"V:|-40-[view(>=400)]-20-|"

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

    NSDictionary *views = NSDictionaryOfVariableBindings(view);

//   self.view   view  添加约束

//    NSLayoutConstraint具体添加约束的类

//    + (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views

//    1.formatVFL  2.opts:同意按照某个方向去布局  3.metrics绑定的参数  4.views  绑定视图的参数

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

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

}

- (void)demo2

{

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

    view.backgroundColor = [UIColor redColor];

    view.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:view];

    

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

    view1.backgroundColor = [UIColor cyanColor];

    view1.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:view1];

    

    NSDictionary *views = NSDictionaryOfVariableBindings(view,view1);

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

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



    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[view(==100)]-100-[view1(==100)]" options:0 metrics:nil views:views]];

//    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view]-100-[view1(>=100)]"  options:0 metrics:nil views:views]];

    


}

- (void)demo3

{

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

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

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

        view.backgroundColor = colorList[i];

        view.tag = i+10;

        view.translatesAutoresizingMaskIntoConstraints = NO;

        [self.view addSubview:view];

    }

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

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

    NSDictionary *views = NSDictionaryOfVariableBindings(redView,yellowView);

//    两个视图横向的约束

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

    

    for (NSString *VFL in constraint) {

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

    }

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

}

- (void)demo4

{

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

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

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

        view.backgroundColor = colorList[i];

        view.tag = i+10;

        view.translatesAutoresizingMaskIntoConstraints = NO;

        [self.view addSubview:view];

    }

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

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

   UIView *blueView = (UIView *)[self.view viewWithTag:12];

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

//    @"H:|-20-[redView(>=200)]-20-|"

//    @"H:|-20-[yellowView(>=100)]-10-[blueView(yellowView)]-20-|"

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

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

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

        

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

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

    }

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


0 0