I have an annoying problem with auto layout that occurs in iOS 8, while the same setup works fine in iOS 7.
I've simplified and isolated the problematic situation, and here is how it should work - and how it does work in iOS 7:
The feature consists of one container view (blue), and three sub views:
- A label
- An image view (with a sliced, scaled image)
- A button
I've added a slider that controls the width of the container view by amending the width constraint of the container.
The auto layout constraints anchors the label to the left-hand-side, the button to the right-hand-side, and the image view is anchored to both sides without having a width constraint.
In iOS 8, the exact same setup looks like this:
Notice how the image view (the black line) appears to be anchored with an offset outside to the right of the container view.
I've tried everything and I can't figure out why this behaves differently in iOS 8. I've removed and replaced all the constraints multiple times but I still get the same result.
These are the constraints set on each of the sub views, respectivly:
And these are the constraints for the container view:
The width constraint of the container view is manipulated by the slider like this:
- (IBAction)handleSliderChange:(id)sender
{
self.buttonWidth.constant = self.slider.value * 1024.0;
[self.containerView setNeedsLayout];
[self.containerView setNeedsUpdateConstraints];
[self.containerView layoutIfNeeded];
[self.view setNeedsLayout];
[self.view setNeedsUpdateConstraints];
[self.view layoutIfNeeded];
}
Others that have had problems with auto layout in iOS 8 seems to have been able to solve similar issues by calling setNeedsLayout
and / or setNeedsUpdateConstraints
directly on the containing view, which is why I'm being very explicit in calling it on both the containing view and the root view. I've tried all combinations of the above, but nothing seems to make a difference.
I'm starting to lose hope of finding a solution, but perhaps someone here has dealt with a similar situation and could bring some clarity into this issue?
Edit: I should add that I've also tried to set constraints from the image view to the label and button as opposed to the edges of the containing view with the same result.
Edit 2: Here's the test project that I've been using when trying to isolate the issue: Download