Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 变量约束_Objective C_Constraints - Fatal编程技术网

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);
}];