I made a UIView
in a StoryBoard containing six UIButtons
.
I wrote some constraints to support landscape mode as well as portrait mode and put them in two different Arrays: portraitModeConstraints
and landscapeModeConstraints
.
When the mode changes, the old constraints are removed and the right array gets added.
When i load the view in portrait mode there is no problem. All Constraints work fine, but wen i turn the iOS-Simulator, i'm getting a lot of warnings. But my constraints work, i tested them. The constraints that make problems are the ones from the autoResizingMask. So i turned them of on my view controller using
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
This made no change. So i debugged a little bit and found out that there is no difference between setTranslatesAutoresizingMaskIntoConstraints:NO
and setTranslatesAutoresizingMaskIntoConstraints:YES
That is really strange and i can't understand this behaviour.
However, i deleted the constraints manually like this:
[self.view removeConstraints:self.view.constraints];
Now, the constraints worked perfectly in both portrait and landscape (the UIButtons were at the right positions) but the background of my UIView
turned black and the background image was gone.
EDIT: When the device will rotate, a function on my viewController
is called:
willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
In there i check wether the toInterfaceOrientation
is landscape or portrait.
When it is portrait, i do this:
[self.view removeConstraints:self.landscapeModeConstraints];
[self.view addConstraints:[NSArray arrayWithArray:self.portraitModeConstraints]];
[self.view layoutSubviews];
And when it is landscape i do this
[self.view removeConstraints:self.portraitModeConstraints];
[self.view addConstraints:[NSArray arrayWithArray:self.landscapeModeConstraints]];
[self.view layoutSubviews];