Let\'s assume I have this layout design on an iPad Portrait.
But I would like to have it this way when the iPad is in landscape:
Is it possible to
My task was the similar in general. I was needing portrait and landscape constraints for both iPhone and iPad. Moreover, yellow and grey views location should be the same in general, but the width of yellow view should be different in iPhone landscape and in iPad landscape (40% of the screen in iPhone and 60% of the screen in iPad):
Constraints for iPhone orientations I've set using traits collections and defining for each constraint for what collection it should be installed. For iPhone there are wChR (portrait) and wChC (landscape). Or wC with hAny:
But for landscape and portrait orientations on iPad the single traits collections are used (wRhR) therefore the way used for iPhone is not suitable. To change constraints for these cases I wares up two constraints sets (the first for iPad in landscape and the second for iPad in portrait):
@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *ipadLandscapeConstraints;
@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *ipadPortraitConstraints;
Note: 1. To do it select several required constraints in storyboard and wire up them to your .m file. To see what constraints were added to array, click ‘+’ button in he left for the property in .m file: 2. I used constraints priority to solve constraints conflicts for iPad
Finally, I’ve implemented configConstraints method to switch constraints sets according to iPad orientation and have overridden viewWillTransitionToSize method:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[self configConstraints];
}
- (void)configConstraints {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPad landscape orientation
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
[NSLayoutConstraint deactivateConstraints:self.ipadPortraitConstraints];
[NSLayoutConstraint activateConstraints:self.ipadLandscapeConstraints];
}
// iPad portrait orientation
else {
[NSLayoutConstraint deactivateConstraints:self.ipadLandscapeConstraints];
[NSLayoutConstraint activateConstraints:self.ipadPortraitConstraints];
}
[self.view layoutIfNeeded];
}
}
May be it would be required to call configConstraints method in other places where the view is loading or appearing, but the base idea is described above.