iOS SDK UIViewContentModeScaleAspectFit vs. UIViewContentModeScaleAspectFill

孤街醉人 提交于 2019-11-30 16:35:54

问题


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.


回答1:


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).




回答2:


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



来源:https://stackoverflow.com/questions/3182649/ios-sdk-uiviewcontentmodescaleaspectfit-vs-uiviewcontentmodescaleaspectfill

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!