1
votes

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];

1
Show the code you're now using to change the constraints on rotation. What do you mean by "now the constraints work perfectly" -- if they did, your view wouldn't be black, and your background image wouldn't be gone.rdelmar
@rdelmar i updated my questionuser2324580
It's hard to tell anything from your posted code. How is the background image of your view added? What constraints does it have? I suspect that you're either not adding them back or at least not all of them -- its size is probably zero. BTW, since portraitModeConstraints is an array, there's no need to use [NSArray arrayWithArray... , just write, [self.view addConstraints:self.portraitModeConstraints];rdelmar
I found a really small mistake in one of my constraints. It works perfectly now. Thanks for the helpuser2324580
what was the small error in your constraints? (having similar issue)bshirley

1 Answers

1
votes

Stack with the same problem, when controller is embedded in nav controller, and i'm doing self.view.setTranslatesAutoresizingMaskIntoConstraints(false) the view is black. Actually there is no reason to set transl..... on self.view. You need to apply it to subviews. Auto Layout Guide