Is there a best practice for using instance variables with NSLayoutConstraint Visual Format?

Say I have an instance variable from a superclass named label, and I want to set automatic layout restrictions using the visual format. If I try to use self.label in a format string, I get parsing errors and I do not have access to _label from a subclass. The workaround that currently works is lower, but it seems 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]]; 
+4
source share
1 answer

constraintsWIthVisualFormat accepts a dictionary of notes, but it should NOT be 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 this, so please let me know if I have an order or syntax, so I can fix it, but the point of your dictionary can be arbitrary.

+9
source

Source: https://habr.com/ru/post/1486094/


All Articles