iOS SDK UIViewContentModeScaleAspectFit vs. UIViewContentModeScaleAspectFill
Asked Answered
E

2

16

I have an image that I would like to display in a UITableViewCell using the UITableViewCellStyleSubtitle style that provides a UIImageView, and two lines of text, plus an optional accessory view.

However, when I set the content mode of the UIImageView to either of UIViewContentModeScaleAspectFit or UIViewContentModeScaleAspectFill, it appears to make no difference. Both of these content modes simply display the entire image, shifting the text to the right to make room for the image.

Does anyone have any examples of how these actually differ in practice? The docs say:

UIViewContentModeScaleAspectFit:
Scales the content to fit the size of the view by maintaining the aspect ratio. Any     remaining area of the view’s bounds is transparent.

UIViewContentModeScaleAspectFill:
Scales the content to fill the size of the view. Some portion of the content may be  clipped to fill the view’s bounds.

But these content modes don't appear to be behaving as advertised.

EDIT: I'm running SDK iOS 4.

Elephantiasis answered 5/7, 2010 at 23:21 Comment(2)
The only thing I can think of is that perhaps UITableViewCell with UITableViewCellStyleSubtitle overrides these content modes to auto-resize the image view to the size of the image being inserted. This makes some sense from results achieved through practice.Elephantiasis
In general, I've found that when UIKit automatically generates how a view should look, it seems like it just sort of takes your parameters and uses them as guidelines. Instead, it goes through some weird proprietary code and can sometimes give you weird output. Sometimes it's easier just to do without the defaults they give you and create the UITableViewCell from scratch.Lamprey
M
9

The reason you don't see a difference between those modes is that the UITableViewCell automatically sizes its image view to fit the aspect ratio of the image. The content modes only come into play when the view's aspect ratio is different from the image's.

Either override layoutSubviews in your custom UITableViewCell class or create an image view yourself (don't use the one that the table view cell provides).

Michelemichelina answered 6/12, 2011 at 1:33 Comment(1)
+1: Doesn't really answer the titular question, but I found this on Google and it helped me! I made a subclass of UITableViewCell which had a UIImageView that I named, naturally enough, imageView. It was jumping around and I couldn't figure out why. Simply renaming it made it so that the underlying UITableViewCell's layoutSubviews method stopped moving my image around.Juvenilia
B
35

I had the same problem. Seems like the Apple documentation is a bit lacking.

I found the answer by searching around the internet. http://www.iphonedevsdk.com/forum/iphone-sdk-development/2973-crop-image.html

Basically,

img1.contentMode = UIViewContentModeScaleAspectFill; 
img1.clipsToBounds = YES;

If you want it to be a bit more fancy, check out this post: UIImageView change Width and Height

Brumley answered 31/5, 2011 at 2:49 Comment(1)
Three damned hours later, clipsToBounds was exactly what I was looking for. Thank you.Harte
M
9

The reason you don't see a difference between those modes is that the UITableViewCell automatically sizes its image view to fit the aspect ratio of the image. The content modes only come into play when the view's aspect ratio is different from the image's.

Either override layoutSubviews in your custom UITableViewCell class or create an image view yourself (don't use the one that the table view cell provides).

Michelemichelina answered 6/12, 2011 at 1:33 Comment(1)
+1: Doesn't really answer the titular question, but I found this on Google and it helped me! I made a subclass of UITableViewCell which had a UIImageView that I named, naturally enough, imageView. It was jumping around and I couldn't figure out why. Simply renaming it made it so that the underlying UITableViewCell's layoutSubviews method stopped moving my image around.Juvenilia

© 2022 - 2024 — McMap. All rights reserved.