3.2 Defining Horizontal and Vertical Constraints with the Visual Format Language

本文介绍如何使用 Auto Layout 的视觉格式语言来定义 iOS 应用中 UI 控件的位置和大小约束,通过具体实例演示如何实现控件间的相对定位,确保应用在不同屏幕尺寸上表现一致。

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

用H:来定义水平方向的约束,用V:来定义垂直方向的约束
下面我们来看几个例子
H:|-100-[btn]-100-|  btn置于父控件的中间左右留空100
H:|-(<=100)-[btn(>=50)]-(<=100)-|  @_@ 自己想象下这会是什么意思
H:|-[btn(>=100,<=200)] 你很聪明的,我知道,所以我也不想多说什么了。

下面是我写的一个小例子
#pragma mark 测试 Visual Format Language
-(void)testVisualFormatLanguage
{
    //第一个控件
    UITextField * textField = [[UITextField alloc] init];
    textField.translatesAutoresizingMaskIntoConstraints = NO;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = @"Email Address";
    [self.view addSubview:textField];
   
    NSString *const kTextFieldHorizontal = @"H:|-[textField]-|";
    NSString *const kTextFieldVertical = @"V:|-[textField]";
   
    NSMutableArray * textFieldConstraints = [[NSMutableArray alloc] init];
    NSDictionary *textFieldDic = NSDictionaryOfVariableBindings(textField);
   
    [textFieldConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kTextFieldHorizontal options:0 metrics:nil views:textFieldDic]];
    [textFieldConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kTextFieldVertical options:0 metrics:nil views:textFieldDic]];
   
   
    //第二个控件
   
    UITextField * textField1 = [[UITextField alloc] init];
    textField1.translatesAutoresizingMaskIntoConstraints = NO;
    textField1.borderStyle = UITextBorderStyleRoundedRect;
    textField1.placeholder = @"Password";
    [self.view addSubview:textField1];

    NSString *const kTextField1Horizontal = @"H:|-[textField1]-|";
    NSString *const kTextField1Vertical = @"V:[textField]-[textField1]";
   
    NSMutableArray * textField1Constraints = [[NSMutableArray alloc] init];
    NSDictionary *textField1Dic = NSDictionaryOfVariableBindings(textField,textField1);
   
    [textField1Constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kTextField1Horizontal options:0 metrics:nil views:textField1Dic]];
    [textField1Constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kTextField1Vertical options:0 metrics:nil views:textField1Dic]];
   
   
    //第三个控件
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.translatesAutoresizingMaskIntoConstraints = NO;
    [btn setTitle:@"OK" forState:UIControlStateNormal];
    [self.view addSubview:btn];
   
    NSString *const kButtonVertical = @"V:[textField1]-[btn]";
   
    NSMutableArray *btnConstraints = [NSMutableArray array];
    NSDictionary * btnDic = NSDictionaryOfVariableBindings(btn,textField1);
   
    [btnConstraints addObject:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:btn.superview attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [btnConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:kButtonVertical options:0 metrics:nil views:btnDic]];
   
   //把这些Contraints 加进来
    NSMutableArray * allContraints = [NSMutableArray array];
    [allContraints addObjectsFromArray:textFieldConstraints];
    [allContraints addObjectsFromArray:textField1Constraints];
    [allContraints addObjectsFromArray:btnConstraints];
    [self.view addConstraints:allContraints];   
}

放到你的代码中,并在viewDidLoad那里调用一下。你就能在界面上看到这三个控件了,别忘了旋转下,换个设备试试效果哦。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值