Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Auto Layout on iOS 8 (code works perfectly on iOS 7)

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!

like image 425
BFar Avatar asked Sep 21 '14 01:09

BFar


Video Answer


1 Answers

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

like image 182
rob mayoff Avatar answered Oct 11 '22 07:10

rob mayoff