3.3 Utilizing Cross View Constraints

本文介绍如何使用Visual Format Language (VFL)来实现iOS应用中不同视图间的约束,具体演示了如何使一个按钮相对于另一个按钮进行水平排列,并确保所有约束正确地应用于它们共同的父视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3.3 Utilizing Cross View Constraints

想象一下这样的效果:
有两个button,分别为topButton,bottomButton。 他们的superView分别为topView,bottomView。 bottomView在topView的下面。现在想让bottomButton的左边对齐topButton的右边,该怎么用VFL实现呢?

我们大概都知道,让bottomButton的左边对齐topButton的右边的VFL表达式的写法 @"H:[topButton]-[bottomButton]"(书上没有中间那一横,不过效果一样)。不过生成的约束该加到那个View里面呢?答案是加到他们共有的父或祖父View里面。

下面的例子是把topView,bottomView加到ViewController的View里面,所以两个button的共有祖父View就是ViewController的View,代码如下:

在ViewDidLoad中加入[self addCrossViewVFL]即可看到结果

#pragma mark 测试 VFL Cross View
-(UIView*)newGrayView
{
    UIView * result = [[UIView alloc] init];
    result.translatesAutoresizingMaskIntoConstraints = NO;
    result.backgroundColor = [UIColor grayColor];
    return  result;
}

-(UIButton *) newButtonOnView:(UIView*)view
{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.translatesAutoresizingMaskIntoConstraints = NO;
    [view addSubview:btn];
    return btn;
}

-(void)addCrossViewVFL
{
    //初始化工作
    UIView * topView = [self newGrayView];
    [self.view addSubview:topView];
    UIView * bottomView = [self newGrayView];
    [self.view addSubview:bottomView];
   
    UIButton * topButton = [self newButtonOnView:topView];
    UIButton * bottomButton = [self newButtonOnView:bottomView];
   
    [topButton setTitle:@"Top Button" forState:UIControlStateNormal];
    [bottomButton setTitle:@"Botton Button" forState:UIControlStateNormal];
   
    //约束 topView
    NSString *const kTopViewH = @"H:|-[topView]-|";
    NSString *const kTopViewV = @"V:|-[topView(==100)]";
   
    NSMutableArray * topViewConstraints = [NSMutableArray array];
   
    NSDictionary *topViewDic = NSDictionaryOfVariableBindings(topView);
    [topViewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kTopViewH options:0 metrics:nil views:topViewDic]];
    [topViewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kTopViewV options:0 metrics:nil views:topViewDic]];
    [topView.superview addConstraints:topViewConstraints];
   
    //约束 bottonView
    NSString *const kbottomViewH = @"H:|-[bottomView]-|";
    NSString *const kbottomViewV = @"V:[topView]-[bottomView(==100)]";
   
    NSMutableArray * bottonViewConstraints = [NSMutableArray array];
   
    NSDictionary *bottonViewDic = NSDictionaryOfVariableBindings(topView,bottomView);
    [bottonViewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kbottomViewH options:0 metrics:nil views:bottonViewDic]];
    [bottonViewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kbottomViewV options:0 metrics:nil views:bottonViewDic]];
    [bottomView.superview addConstraints:bottonViewConstraints];
   
    //约束topButton
    NSString *const ktopButtonH = @"H:|-[topButton]";
   
    NSMutableArray * topButtonConstraints = [NSMutableArray array];
   
    NSDictionary *topButtonDic = NSDictionaryOfVariableBindings(topButton);
    [topButtonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:ktopButtonH options:0 metrics:nil views:topButtonDic]];
    [topButtonConstraints addObject:[NSLayoutConstraint constraintWithItem:topButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:topButton.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    [topView.superview addConstraints:topButtonConstraints];
   
    //约束bottomButton
    [bottomButton.superview addConstraint:[NSLayoutConstraint constraintWithItem:bottomButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:bottomButton.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];//垂直方向居中
   
    NSString *const kBottomButtonV = @"H:[topButton]-[bottomButton]";
    NSDictionary * bottomButtonDic = NSDictionaryOfVariableBindings(topButton, bottomButton);
    [bottomView.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:kBottomButtonV options:0 metrics:nil views:bottomButtonDic]];//水平方向挨在topButton后面
       
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值