I need to add an undefined number of NSButton
to an NSView in code. The problem is that I can' t use constraintsWithVisualFormat:
because i don' t know the name of the NSButton
and also the number of button that I have. Anyone have a solution? Thanks!
1 Answer
When using constraintsWithVisualFormat:
, you need to know the names of the variables that point to your NSButtons only if you use NSDictionaryOfVariableBindings
to create the dictionary of views. You could just as easily build your own dictionary using whatever keys you like.
If your buttons are stored in an array, you can iterate through them and create constraints between each of them:
for ( int i = 1 ; i < buttonArray.count ; i++ ) {
NSDictionary* views = @{ @"buttonOne":buttonArray[i-1] , @"buttonTwo":buttonArray[i] } ;
NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[buttonOne]-[buttonTwo]" options:0 metrics:nil views:views] ;
// Use the constraints.
}
-
Yes, I found the solution before your reply but this is exactly what I need. Thanks again!– LucaCommented Jun 3, 2013 at 15:36
constraintsWithVisualFormat:
method but can I do that if I have an array ofNSButton
?