19

I have an app that was built using auto-layout and everything still works as expected when I run it on my iOS 7 device, however when I run it on an iOS 8 device some of my constraints go a little wacky.

I actually experienced a similar issue in another app but it wasn't a big issue. Now, I'm starting to wonder if it is a bug in the iOS 8 SDK or if there is a new way of handling auto layout in iOS8 that I'm not aware of.

Here is my setup: I have a UIView with a UIImageView embedded inside it. Both the view and the image view have constraints pinning their bottom, leading & trailing edges to their superviews with constant = 0.

The UIView also has its top edge pinned to its superview and I have an IBOutlet connected to the constraint in order to adjust it programatically. The UIImageView has a fourth constraint fixing its height to the height of the device (in my case 568).

Here is the behavior I am expecting/achieving in iOS 7: I am trying to shrink the height of the container view and cause the top of the image view to be cut off w/o the aspect ratio changing (sort of looks like you cropped the top of the image off)...this is why I have pinned the height of the image view and the bottom edge.

What actually happens in iOS8: The container view shrinks as expected (it stays pinned to the sides & bottom & the height shrinks). The UIImageView behaves as if it had a top space constraint with cosntant == 0. Instead of cutting off the top of the image, the whole image shrinks down. Since I have the image in AspectFit mode, the sides pinch in as well to keep the aspect ratio the same (but the imageView itself remains pinned to the leading, trailing & bottom edges as it should).

How I do it: I have a notification that fires from another part of my code to adjust the top-space constraint hooked up to the IBOutlet. The method it calls is really quite simple:

- (void) setTopSpaceForContainerView:(NSNotification*) notif{
    containerView_TopSpace.constant = [[notif.userInfo objectForKey:kTopSpace] intValue];
    [self.view setNeedsUpdateConstraints];
    [self.view setNeedsLayout];
}

Anyone else have a similar experience? I'm trying to find a workaround, but iOS 8 just seems determined to shrink my image.

Thanks a ton!

2

2 Answers 2

10

This is definitely an iOS 8 bug. I was able to work around it by sending setNeedsLayout to the container view (the superview of the image view).

I see that you're sending setNeedsLayout to self.view. If that code is in your view controller, then I guess self.view is not the container view. Try sending setNeedsLayout to the container view directly.

Demo project is here.

enter image description here

12
  • It works! +1 more because this is backwards compatible for iOS 7 as well. Which makes me wonder, in iOS7 calling setNeedsUpdateConstraints and setNeedsLayout on the root view automatically trickled down to all the subviews. Now we need to call setNeedsLayout specifically on the views that have constraints changing...that seems like it could reduce unnecessary layout calculations and speed things up. Should I have been doing it this way all along?
    – BFar
    Commented Sep 26, 2014 at 23:41
  • I don't think it trickled down in the way you think. I think that in iOS 7, changing the constraint constant flagged the constrained views as needing layout (or triggered the constraint solver in some other way), and in iOS 8 it doesn't.
    – rob mayoff
    Commented Sep 26, 2014 at 23:54
  • Yes, that makes a lot of sense. Sounds like it is time for many of us to go fix all of our setNeedsLayout calls to point to the views that have a changing constraint.
    – BFar
    Commented Sep 27, 2014 at 0:00
  • I'm really excited to try this on my problem and see if things sort out: stackoverflow.com/questions/25987244/… If so, I'll be back to mark this puppie UP!
    – Gallymon
    Commented Sep 27, 2014 at 4:45
  • 4
    The radar has been picked up by Apple and fixed. It is an iOS 8 bug. See twitter.com/mairwatching/status/517747734564990976 Here are more details: twitter.com/mairwatching/status/517748417452208129
    – smileyborg
    Commented Oct 3, 2014 at 17:22
-1

Try using UIViewContentModeScaleAspectFill and setClipsToBounds:NO

1
  • Still having the same issue :/
    – BFar
    Commented Sep 24, 2014 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.