iOS autolayout 代碼,自定義間距

最近換了新的項目組,而後這個項目組是純代碼,而後我就開始試着用代碼去寫適配,結果學藝不精,遇到個閃退,搜了一下發現幾乎沒有人遇到這個問題,後來發現其實就是我本身太粗心了。app

 

我是這樣寫的spa

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

 

報錯信息是這樣的3d

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?code

其實報錯信息說的很明白,就是我寫了一個動態的間距hpadding,而後他沒識別;orm

看到這不少大神應該都明白了,由於我把hpaddingKeyValue加錯地方了blog

我加在了Views裏面,其實應該加在metrics就對啦,仍是stackoverflow比較強大呀。最後在裏面搜到的答案it

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

以上就對啦。io

相關文章
相關標籤/搜索