iOS SDK UIViewContentModeScaleAspectFit vs. UIViewContentModeScaleAspectFill
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 开发者_JS百科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.
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
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).
精彩评论