For an app (iOS 7 compatible) I need to have the possibility for totally different layouts in landscape and portrait mode. I see the only way in setting the constraints programmatically and change them on rotation. So I set my interface using IB and then set the constraints programmatically.
I saw somewhere the approach to remove first all constraints, then create and add new constraints and then set the update flag for constraints on the parent view. I want the elements just to be full width of the screen and one underneath the other:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
self.landscapeConstraints = [NSMutableArray new];
self.portraitConstraints = self.view.constraints;
//creation of the viewsDictionary
[self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[segmentedControl]-|" options:0 metrics:nil views:viewsDictionary]];
[self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[fullText]-|" options:0 metrics:nil views:viewsDictionary]];
[self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[bubbleTitle]-|" options:0 metrics:nil views:viewsDictionary]];
[self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[bubble]-|" options:0 metrics:nil views:viewsDictionary]];
[self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[chartView]-|" options:0 metrics:nil views:viewsDictionary]];
[self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(20)-[segmentedControl]-[fullText]-[bubbleTitle]-[bubble]-[chartView]|" options:0 metrics:nil views:viewsDictionary]];
[self.view removeConstraints:self.portraitConstraints];
[self.view addConstraints:self.landscapeConstraints];
[self.view setNeedsUpdateConstraints];
}
My question: Is this way complete and correct?
I come to this question, since the elements are too big after the rotation. It looks like the size of the superview is not correct after rotation. All elements are therefore too big and it seems that the biggest element (a long text label) is giving the width, which is a lot bigger than the screen. If I set one of the elements with a size in the constraints (eg. [segmentedControl(==500)] then the size of this element is ok and ALL other elements also have that size.
The command
NSLog(@"%@", [self.view performSelector:@selector(_autolayoutTrace)]);
gives me something like this for all elements:
*UIView:0x7f9a38f36510- AMBIGUOUS LAYOUT for UIView:0x7f9a38f36510.minX{id: 9}, UIView:0x7f9a38f36510.minY{id: 12}, UIView:0x7f9a38f36510.Width{id: 15}, UIView:0x7f9a38f36510.Height{id: 17}
Can anyone hint me where I am doing wrong? I am working on this for hours now...
EDIT
What I really don't understand: It seems the solution after I removed all constraints would be to define the size of the superview also by a constraint like this:
NSDictionary *viewsDictionary = @{ @"view":self.view};
[self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[view(360)]" options:0 metrics:nil views:viewsDictionary]];
[self.landscapeConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(650)]" options:0 metrics:nil views:viewsDictionary]];
Otherwise all views collapse, since the superview seems to have zero-size. And even with these additional constraints the view sits too far left (about 20px)!
Can anyone explain that to me? Do I really need to generate that constraint manually with the current size of the windows?? I would expect the containing view (which is the main window) to have the size of the full screen.