简体   繁体   English

iOS SDK UIViewContentModeScaleAspectFit与UIViewContentModeScaleAspectFill

[英]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. 我有一个图像,我想在UITableViewCell显示使用UITableViewCellStyleSubtitle样式,提供UIImageView ,两行文本,以及一个可选的附件视图。

However, when I set the content mode of the UIImageView to either of UIViewContentModeScaleAspectFit or UIViewContentModeScaleAspectFill , it appears to make no difference. 但是,当我将UIImageView的内容模式设置为UIViewContentModeScaleAspectFitUIViewContentModeScaleAspectFill ,它似乎没有任何区别。 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. 编辑:我正在运行SDK iOS 4。

I had the same problem. 我有同样的问题。 Seems like the Apple documentation is a bit lacking. 似乎Apple文档有点缺乏。

I found the answer by searching around the internet. 我在网上搜索找到了答案。 http://www.iphonedevsdk.com/forum/iphone-sdk-development/2973-crop-image.html 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 如果你想要它更有趣,请看看这篇文章: UIImageView改变宽度和高度

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. 您没有看到这些模式之间的差异的原因是UITableViewCell自动调整其图像视图的大小以适合图像的宽高比。 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). 要么在自定义UITableViewCell类中覆盖layoutSubviews ,要么自己创建一个图像视图(不要使用表视图单元提供的那个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM