Reputation: 4159
Given the fact that we have only common size class for both iPad Portrait and iPad Landscape, how do you finally customize your Autolayout behaviour/constraints for iPad for different orientations?
I want to have something like this in Portrait:
And this in Landscape:
Upvotes: 2
Views: 2042
Reputation: 7667
In iOS 8 the easy way (for the moment) is to use viewWillTransitionToSize:withTransitionCoordinator
Here you have an example with your request using the above notification testLandscapePortrait_iPad
Upvotes: 2
Reputation: 104082
In iOS 8, you can use viewWillTransitionToSize:withTransitionCoordinator:, to be notified when the controller is being rotated. Use the ratio of height to width to determine which orientation you are moving to, and adjust your constraints accordingly. You could make IBOutlets to the view's height and width constraints, and change their constant values inside that method.
Another way to do this is to use the multiplier and constant together in a way that automatically adjusts the constraints on rotation. The formula for a relative width constraint, for example, would be:
subview.width = superview.width * multiplier + constant
So, for example, if you wanted the view full width (768) in portrait and 350 in landscape, you would solve these two equations,
768 = 768 * m + c and 350 = 1024*m + c
Solving those, gives you m= -1.6328 and c= 2022
To use this, you would give your subview a width constraint in IB, but check the "remove at build time" box so it will be removed and replaced with the one you add in code.
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.subView attribute:NSLayoutAttributeWidth relatedBy:0 toItem:self.view attribute:NSLayoutAttributeWidth multiplier:-1.6328 constant:2022]];
}
Normally, you could do this all in IB, but only if the multiplier is positive; I've tried it with these numbers, and the system won't allow me to use a negative number for the multiplier.
Upvotes: 2