How do I create a 2 x 2 grid of UIViews programmatically using constraints in objective-c?
For example, I have four UIViews with their names corresponding to their row/column position (oneOne, oneTwo, twoOne, twoTwo) as per the below image.
Here is the code I am currently using:
-(void)buildGrid {
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
UIView *oneOne = [UIView new];
[oneOne setBackgroundColor:[UIColor redColor]];
[oneOne setTranslatesAutoresizingMaskIntoConstraints:NO];
UIView *oneTwo = [UIView new];
[oneTwo setBackgroundColor:[UIColor greenColor]];
[oneTwo setTranslatesAutoresizingMaskIntoConstraints:NO];
UIView *twoOne = [UIView new];
[twoOne setBackgroundColor:[UIColor blueColor]];
[twoOne setTranslatesAutoresizingMaskIntoConstraints:NO];
UIView *twoTwo = [UIView new];
[twoTwo setBackgroundColor:[UIColor purpleColor]];
[twoTwo setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:oneOne];
[self.view addSubview:oneTwo];
[self.view addSubview:twoOne];
[self.view addSubview:twoTwo];
NSDictionary *viewsDict = NSDictionaryOfVariableBindings(oneOne, oneTwo, twoOne, twoTwo);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[oneOne]-10-[twoOne]-10-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[oneOne]-10-[twoOne]-10-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[oneTwo]-10-[twoTwo]-10-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[twoOne]-10-[twoTwo]-10-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:viewsDict]];
}
I have followed several tutorials but none (so far) have described how to create this kind of 'grid' layout, they have been far simpler like a single 'column' of UIViews but when I had further 'columns' I get errors about not being able to maintain the constraints:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
This code works fine if I make it four UIViews going down or accross the screen but not if I make it 2 x 2. What am I doing wrong?
Update
As per accepted answer the constraints should have been:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[oneOne]-10-[twoOne(==oneOne)]-10-|"
options:0
metrics:nil
views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[oneTwo]-10-[twoTwo(==oneTwo)]-10-|"
options:0
metrics:nil
views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[oneOne]-10-[oneTwo(==oneOne)]-10-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[twoOne]-10-[twoTwo(==twoOne)]-10-|"
options:0
metrics:nil
views:viewsDict]];

MasonryorKeepLayout- it will be easier to create complex layouts from code with them.