Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Let's say I have an instance variable from a superclass named label, and I want to set auto layout constraints using the visual format. If I try to use self.label in the format string, I get parse errors, and I don't have access to _label from a subclass. The workaround that is currently working is below, but it seems kind of ugly. Is there a better way?

    UILabel *label = self.label;
    NSDictionary *views = NSDictionaryOfVariableBindings(label, _textField);

    [self.contentView addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label(==_textField)][_textField(==label)]-|"
                                             options:NSLayoutFormatAlignAllCenterY
                                             metrics:nil
                                               views:views]];
share|improve this question

1 Answer 1

up vote 7 down vote accepted

constraintsWIthVisualFormat takes a views dictionary but it does not HAVE to come from NSDictionaryOfVariableBindings For example:

UILabel *label = self.label;
NSDictionary *views = @{@"label":self.label, @"_textField":_textField};

[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label(==_textField)][_textField(==label)]-|"
                                         options:NSLayoutFormatAlignAllCenterY
                                         metrics:nil
                                           views:views]];

I have not tested that so please let me know if I have the order or the syntax wrong so I can fix it, but the point is your dictionary can be arbitrary.

share|improve this answer
    
Good point, TY. –  Mike Akers Jun 13 '13 at 20:54
    
This is the way to deal with VFL when all you have is a property. I have written about this in some detail here if that helps. –  jrturton Jun 13 '13 at 22:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.