AutoLayout NSLayoutConstraint VFL

- (void)viewDidLoad

{

    [super viewDidLoad];

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];

    [button1 setTitle:@"Button1" forState:UIControlStateNormal];

    button1.backgroundColor = [UIColor grayColor];

    [self.view addSubview:button1];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];

    [button2 setTitle:@"Button2" forState:UIControlStateNormal];

    button2.backgroundColor = [UIColor grayColor];

    [self.view addSubview:button2];

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];

    [button3 setTitle:@"Button3" forState:UIControlStateNormal];

    button3.backgroundColor = [UIColor grayColor];

    [self.view addSubview:button3];

    

    //1. 关掉Autoresizing

    button1.translatesAutoresizingMaskIntoConstraints = NO;

    button2.translatesAutoresizingMaskIntoConstraints = NO;

    button3.translatesAutoresizingMaskIntoConstraints = NO;

    //2. VFL

    NSString *vfl = @"|-left-[button1]-10-[button2(==button1)]-10-[button3(==button1)]-right-|";

    //3. 创建约束

    //NSDictionary *views = @{@"button1" : button1, @"button2" : button2, @"button3" : button3};

    NSDictionary *views =  NSDictionaryOfVariableBindings(button1, button2, button3);

    NSDictionary *metricss = @{@"left" : @10, @"right" : @10};

    

    NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:vfl options:NSLayoutFormatAlignAllCenterY metrics:metricss views:views];

    //4. 将约束加入到父视图

    [self.view addConstraints:constraints1];

    // V:表示垂直高度

    vfl = @"V:|-200-[button1]";

    NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:vfl options:0 metrics:nil views:views];

    [self.view addConstraints:constraints2];

    

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值