I have an issue when using autolayout (I'm new to it) where, although my constraints function as expected (everything centered horizontally, vertical spacing as I want it), when I move to landscape orientation, the bottom button disappears.
I understand that this happens because I've constrained my objects based on a portrait orientation view, and this no longer applies when the height and width values shift as we move to landscape. I just don't really know how to account for these changes when changing orientation. Any advice?
code and screenshot below:
-(void)setConstraints {
[self.view removeConstraints:self.view.constraints];
UIButton *cameraButton = self.cameraButton;
UILabel *camera = self.videoLabel;
UIButton *libraryButton = self.libraryButton;
UILabel *library = self.libraryLabel;
NSDictionary *views = NSDictionaryOfVariableBindings(camera, cameraButton, libraryButton, library);
NSDictionary *metrics = @{@"horizontalSpacing":@500.0, @"verticalSpacing":@125};
//set up top button to be horizontally centered
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[cameraButton]-|"
options:0
metrics:nil
views:views];
//set up top button vertical from top of superview
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"V:|-175-[cameraButton]"
options:0
metrics:nil
views:views]];
//set up top button label to be horizontally centered
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"|-[camera]-|"
options:0
metrics:nil
views:views]];
//set up second button to be horizontally centered
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"|-[libraryButton]-|"
options:0
metrics:nil
views:views]];
//set up label for second button to be horizontally centered
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"|-[library]-|"
options:0
metrics:nil
views:views]];
//set up vertical constraints by spacing ALL objects appropriately
constraints = [constraints arrayByAddingObjectsFromArray:
[NSLayoutConstraint constraintsWithVisualFormat: @"V:[cameraButton]-[camera]-verticalSpacing-[libraryButton]-[library]"
options:0
metrics:metrics
views:views]];
self.libraryLabel.textAlignment = NSTextAlignmentCenter;
self.videoLabel.textAlignment = NSTextAlignmentCenter;
self.libraryLabel.backgroundColor = [UIColor redColor];
[self.view addConstraints:constraints];
}