Objective c 变量约束
有没有办法把这个Objective c 变量约束,objective-c,constraints,Objective C,Constraints,有没有办法把这个NSUInteger: NSUInteger number = 20; 在这个约束下 [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-25-[btn1]-25-[btn2(==btn1)]-25-[btn3(==btn1)]-25-[btn4(==btn1)]-25-[btn5(==btn1)]-25-|" options:0 metrics:nil views:v
NSUInteger
:
NSUInteger number = 20;
在这个约束下
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-25-[btn1]-25-[btn2(==btn1)]-25-[btn3(==btn1)]-25-[btn4(==btn1)]-25-[btn5(==btn1)]-25-|" options:0 metrics:nil views:views]];
所以我想把25的位置设为20,但应该用数字来表示
注意:我希望最终产品使用可变宽度:
NSUInteger widthOfScreen = [UIScreen mainScreen].bounds.size.width;
NSUInteger width = (widthOfScreen - 55 * 5) / 6;
这就是metrics
参数的作用。您将传递一个命名度量的字典,如下所示:
@{@"number": 20}
然后,在可视化格式中,使用该命名度量,而不是硬编码的数值
@"H:|-number-[btn1]-number-[btn2(==btn1)]-number-[btn3(==btn1)]-number-[btn4(==btn1)]-number-[btn5(==btn1)]-number-|"
如果您还有时间,请使用另一个选项。它缩短了构建和修改约束所需的开发时间。对我来说,这是唯一的出路。变量只能传递到constraint maker块中
[self.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(number).with.offset(10);
make.bottom.equalTo(@20).with.offset(10);
make.left.equalTo([NSNumber numberWithFloat:20.0f]).with.offset(10).with.priorityHigh();
make.right.eqalTo(number);
}];