How to write NSConstraint visual format language by including another button's width plus a constant

Deadly 提交于 2019-12-07 02:37:02

问题


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

my question: is there a way to make button1 equal to button 2's with + a constant.. I tried:

[button1(==button2+10)]

and

[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)]
                         ^'

(I obviously know you can do this by doing an NSStringWithFormat and simply filling in the variable in question.. but that looks too messy)

ideas?

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)


回答1:


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


来源:https://stackoverflow.com/questions/20019730/how-to-write-nsconstraint-visual-format-language-by-including-another-buttons-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!