如何通过包含另一个按钮的宽度加上常量来编写NSConstraint可视化格式语言 [英] How to write NSConstraint visual format language by including another button's width plus a constant
问题描述
在 docs 中关于 NSLayoutConstraint的可视化格式语言
有一个例子,你指定button1的宽度等于button2:
In the docs about the visual format language for NSLayoutConstraint
there is an example where you specify button1 is equal in width to button2:
[button1(==button2)]
我的问题:是有一种方法可以使button1等于按钮2的+ +常量..我试过:
my question: is there a way to make button1 equal to button 2's with + a constant.. I tried:
[button1(==button2+10)]
和
[button1(==(button2+10))]
失败..这是一个错误示例:
and both failed.. here is an error example:
A predicate on a view's thickness must end with ')' and the view must end with ']'
V:[tagWrapper(==tagButton+10)]
^'
(我显然知道你可以做到这一点做一个NSStringWithFormat并简单填写有问题的变量..但这看起来太乱了)
(I obviously know you can do this by doing an NSStringWithFormat and simply filling in the variable in question.. but that looks too messy)
想法?
PS万一你好奇我为什么要坚持使用视觉格式语言(而不是像这样的其他方式) / a>回答..或者使用包装库。查看此代码示例)
P.S. just in case you're curious why I would like to stick with visual format language (as opposed to the other ways of doing it like this answer.. or using wrapper libraries out there.. check out this code sample)
推荐答案
无法使用可视格式语言指定某些约束。您可以使用这样的简单约束:
Some constraints can't be specified with the visual format language. You could use a simple constraint like this:
NSLayoutConstraint *c;
c = [NSLayoutConstraint constraintWithItem:button1
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:button2
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:10.0];
这篇关于如何通过包含另一个按钮的宽度加上常量来编写NSConstraint可视化格式语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!