简体   繁体   English

UICollectionView UIImage with.aspectFit 正在改变约束

[英]UICollectionView UIImage with .aspectFit is changing contraints

I have a very simple collection view that has images in it, my Cell class looks like this:我有一个非常简单的集合视图,里面有图像,我的 Cell class 看起来像这样:

class ImageListCell: UICollectionViewCell {
    @IBOutlet weak var btnDelete: UIButton! {
        didSet {
            btnDelete.layer.cornerRadius = btnDelete.frame.height / 2
        }
    }
    @IBOutlet weak var imageView: UIImageView!
    
    @IBAction func btnDeleteTap(_ sender: Any) {
        print("Delete image")
    }
}

The cell's imageView constraints have some padding at the top and trailing edge:单元格的 imageView 约束在顶部和尾部边缘有一些填充:

在此处输入图像描述

If I color the ImageListCell background red and the imageView background green, this is what it looks like:如果我将 ImageListCell 背景涂成红色,将 imageView 背景涂成绿色,则它看起来像这样:

在此处输入图像描述

Which is 100% correct!这是 100% 正确的!

However, if all I do is add an image to imageView the contraints get all goofy?但是,如果我所做的只是将图像添加到 imageView,那么约束会变得很愚蠢吗?

在此处输入图像描述

This only happens in.aspectFit, if I change it to.aspectFill it works but the images are all stretched.这只发生在 .aspectFit 中,如果我将它更改为 .aspectFill 它可以工作,但图像都被拉伸了。

在此处输入图像描述

I made sure I'm not adjusting cell size or anything and Estimated Size on the collection view is turned off.我确保我没有调整单元格大小或任何东西,并且关闭了集合视图上的Estimated Size

Does anyone have any idea why.aspectFill is overriding my constraints?有谁知道为什么.aspectFill 会覆盖我的约束?

  • Nothing is happening to your constraints.您的约束没有任何变化。 The image view is exactly where you put it.图像视图正是您放置它的位置。

  • In the first picture-with-the-image, .aspectFit is behaving exactly as expected: no empty space is left within the image view around the outside of the image, and the image is not distorted.在第一张带图像的图片中, .aspectFit的行为完全符合预期:图像外部周围的图像视图中没有留下任何空白区域,并且图像没有失真。 If you don't like the fact that the image is "leaking" off the right side, that's merely because you forgot to set your image view's clipsToBounds to true .如果您不喜欢图像从右侧“泄漏”的事实,那仅仅是因为您忘记将图像视图的clipsToBounds设置为true

  • The second picture-with-the-image is not .aspectFill as you claim.第二张带图像的图片不是您声称的.aspectFill It is .scaleToFill .它是.scaleToFill There is a huge difference.这是个很大的差异。 So your statement of the phenomena is not even correct.所以你对现象的陈述甚至都不正确。 .scaleToFill can distort the image; .scaleToFill可以扭曲图像; .aspectFill would not do that (but it would display the green background of the image view). .aspectFill不会那样做(但它会显示图像视图的绿色背景)。

There are a few things in your question that seem incorrect.您的问题中有几处似乎不正确。

You are referring to content modes with values such as .aspectFit and .aspectFill .您指的是具有.aspectFit.aspectFill的内容模式。 In Swift code there are no such values.在 Swift 代码中没有这样的值。 These would be .scaleAspectFit and .scaleAspectFill .这些将是.scaleAspectFit.scaleAspectFill In a storyboard the "Content Mode" property of a view can be things like "Aspect Fit", "Aspect Fill", and others.在 storyboard 中,视图的“内容模式”属性可以是“Aspect Fit”、“Aspect Fill”等。

Since you setup your cell in a storyboard I'll refer to the storyboard property settings.由于您在 storyboard 中设置了单元格,因此我将参考 storyboard 属性设置。 Everything I describe applies to the UIImageView outlet in your collection view cell.我描述的所有内容都适用于您的集合视图单元格中的 UIImageView 插座。

The first screenshot you show with what you claim to be as .aspectFit would only happen if you set the "Content Mode" to "Aspect Fill" (not "Aspect Fit" as you claim) and you turned off the "Clips to Bounds" setting.只有当您将“内容模式”设置为“Aspect Fill”(而不是您声称的“Aspect Fit”)并且关闭“Clips to Bounds”时,您显示的第一个屏幕截图才会显示您声称的.aspectFit环境。 With "Aspect Fill" set, a wide image will correctly fill the height of the image view (which is why you still see the top red border, but the left and right sides of the image would overflow outside the image view (which is why you can't see the right red border). If "Clip to Bounds" was checked, the right red border would be visible. The collection view cell itself clips all of its content which prevents the overflowing image from spilling out of the cell's frame.设置“Aspect Fill”后,宽图像将正确填充图像视图的高度(这就是为什么您仍然看到顶部红色边框,但图像的左侧和右侧会溢出图像视图的原因(这就是为什么你看不到右边的红色边框)。如果选中“Clip to Bounds”,则右边的红色边框将可见。集合视图单元格本身会剪辑其所有内容,从而防止溢出的图像溢出单元格的框架.

The second screenshot you show with what you claim to be as .aspectFill would be the result of setting "Content Mode" to "Scale To Fill", not "Aspect Fill".您显示的第二个屏幕截图与您声称的.aspectFill将是将“内容模式”设置为“缩放以填充”而不是“纵横比填充”的结果。 The result would be the same whether "Clip to Bounds" was checked or not.无论是否选中“Clip to Bounds”,结果都是一样的。 "Scale to Fill" distorts the image as needed to fully fill the height and width of the image view. “缩放以填充”根据需要扭曲图像以完全填充图像视图的高度和宽度。

Based on your description you want to set "Content Mode" to "Aspect Fit".根据您的描述,您希望将“内容模式”设置为“Aspect Fit”。 While not really needed for "Aspect Fit", it wouldn't hurt to enable "Clip to Bounds".虽然“Aspect Fit”并不是真正需要的,但启用“Clip to Bounds”也没什么坏处。 This will show the entire image with appropriate green borders and no distortion.这将显示具有适当绿色边框且没有失真的整个图像。

tl;dr - your constraints are fine. tl; dr - 你的约束很好。 You are not really setting the image view's content mode to "Aspect Fit" in the first image picture.您并没有真正将图像视图的内容模式设置为第一张图像中的“Aspect Fit”。 You are setting it to "Aspect Fill" and "Clip to Bounds" is off.您将其设置为“Aspect Fill”并且“Clip to Bounds”已关闭。

If you want to do this in code you want:如果您想在代码中执行此操作:

imageView.contentMode = .scaleAspectFit
imageView.clipsToBounds = true // optional for .scaleAspectFit

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

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