Re: UIImageView Scaling

Steve Christensen <punster@...>

The docs for that section do reflect the behavior, which you can see where the imageView size was set to the image size pre-layout by calling sizeToFit(). The problem is that in the auto layout world, constraints have the last word in how views are ultimately sized unless one or both sides of a view's edge constraints is not set so the edge is floating.

In the better-behaved cases 1 and 3, you can see that happen at the pre- and post-layout points. Case 2 shows the same behavior once setNeedsLayout() was added so that a layout pass would be scheduled for a bit in the future outside of that weird timing-issue period.

On Jul 31, 2018, at 3:55 PM, Rick Aurbach via Groups.Io <rlaurb@...> wrote:


I haven’t explored your argument in a test environment, but the reason I said the call to setNeedsLayout() was in ‘addition’ to the call to sizeToFit() is the following excerpt from the documentation of UIImageView (color enhancement is mine):

var image: UIImage?

The image displayed in the image view.


var image: UIImage? { get set }


This property contains the main image displayed by the image view. This image is displayed when the image view is in its natural state. When highlighted, the image view displays the image in its highlightedImage property instead. If that property is set to nil, the image view applies a default highlight to this image. If the animationImages property contains a valid set of images, those images are used instead.

Changing the image in this property does not automatically change the size of the image view. After setting the image, call the sizeToFit() method to recompute the image view’s size based on the new image and the active constraints. 

This property is set to the image you specified at initialization time. If you did not use the init(image:) or init(image:highlightedImage:) method to initialize your image view, the initial value of this property is nil.

So, at an absolute minimum, this is a documentation issue…



Join { to automatically receive all group messages.