iOS autolayout 代码,自定义间距

最近换了新的项目组,然后这个项目组是纯代码,然后我就开始试着用代码去写适配,结果学艺不精,遇到个闪退,搜了一下发现几乎没有人遇到这个问题,后来发现其实就是我自己太粗心了。

我是这样写的

NSArray *constraints2=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-hPadding-[_productNewLabel(==13)]" options:0 metrics:nil views:@{@"_productNewLabel":_productNewLabel,@"hPadding":@(hPadding)}];

报错信息是这样的

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: It's not possible to set a space equal to the width or height of a view. Perhaps you want to use a view as a spacer?

其实报错信息说的很明白,就是我写了一个动态的间距hpadding,然后他没识别;

看到这很多大神应该都明白了,因为我把hpaddingKeyValue加错地方了

我加在了Views里面,其实应该加在metrics就对啦,还是stackoverflow比较强大呀。最后在里面搜到的答案

NSArray *constraints2=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-hPadding-[_productNewLabel(==13)]" options:0 metrics:@{@"hPadding":@(hPadding)} views:@{@"_productNewLabel":_productNewLabel}];

以上就对啦。

原文地址:https://www.cnblogs.com/waiwaibuzhidao/p/6093587.html