简体   繁体   English

AutoLayout:UIImageView和Aspect Fit内容模式

[英]AutoLayout: UIImageView and Aspect Fit content mode

I'm trying to place an UIImageView in the parent view's center. 我正在尝试在父视图的中心放置一个UIImageView Image must keep its original aspect ratio and also it shouldn't exceed parent's bounds. 图像必须保持其原始宽高比,并且不应超过父级边界。 So landscape images should be limited by parent's width and portrait images should occupy as much vertical space as needed keeping original ratio. 因此,景观图像应受父母宽度的限制,肖像图像应占据所需的垂直空间,保持原始比例。

Sounds like quite a simple task for AutoLayout, right? 听起来像AutoLayout的一个简单的任务,对吧? Here's my setup for UIImageView : 这是我对UIImageView的设置:

  • center vertically in parent 父母垂直居中
  • center horizontally in parent 父母水平居中
  • imageView.width <= superview.width imageView.width <= superview.width
  • imageView.height <= superview.height imageView.height <= superview.height

I also set contentMode to Aspect Fit . 我还将contentMode设置为Aspect Fit Everything works really great for small images which are smaller than device screen but for some reason my UIImageView takes more space than its underlying UIImage for large images (notice the green background - imageView.backgroundColor = [UIColor greenColor] ). 一切真正伟大的作品为它比设备屏幕较小,但由于某种原因,我的小图像UIImageView需要更多的空间比其基本UIImage大型图像(注意绿色背景- imageView.backgroundColor = [UIColor greenColor] )。

在此输入图像描述

Why is this happening? 为什么会这样? I am not an AutoLayout expert by my constraints look reasonable to me. 我不是一个AutoLayout专家,我的约束对我来说是合理的。 If I use smaller images like 200x400 then UIImageView takes exactly 200x400 points on the screen with no extra green areas. 如果我使用像200x400这样的较小图像,那么UIImageView在屏幕上只需200x400点,没有额外的绿色区域。

I'd like to add borders and rounded corners which obviously won't work properly in this case. 我想添加边框和圆角,在这种情况下显然无法正常工作。

This is happenning because you've set width and height constraints as <=. 这是因为您将宽度和高度约束设置为<=。 For small images imageView's frame is calculated without any issues and all constraints are satisfied. 对于小图像,imageView的帧计算没有任何问题,并且满足所有约束。 But when a big image is set, imageView's size is going to be set so that the image fits, but at the same it is limited by the size of the superview (screen size). 但是当设置一个大图像时,将设置imageView的大小以使图像适合,但同时它受到超视图(屏幕大小)大小的限制。

  • If you know aspect ratio for sure, you can set it as a constraint and you won't see any green background. 如果你肯定知道纵横比,你可以将它设置为约束,你将看不到任何绿色背景。
  • Otherwise just leave your constraints as they are and set background color to clear color. 否则只需保留约束,并设置背景颜色以清除颜色。 This way any unoccupied zones will be transparent and any image you set will take maximum space in at least one dimension. 这样,任何未占用的区域都将是透明的,您设置的任何图像将至少在一个维度上占用最大空间。

Edit: 编辑:

Probably not the best solution, kind of oldschool. 可能不是最好的解决方案,有点老了。 You can add strict width and height constraints and calculate them manually using image's aspectRatio: 您可以添加严格的宽度和高度约束,并使用image的aspectRatio手动计算它们:

@IBOutlet weak var heightConstraint: NSLayoutConstraint!
@IBOutlet weak var widthConstraint: NSLayoutConstraint!

func setImage(image: UIImage) {
    imageView.image = image
    let screenSize = UIScreen.main.bounds.size

    let imageAspectRatio = image.size.width / image.size.height
    let screenAspectRatio = screenSize.width / screenSize.height

    if imageAspectRatio > screenAspectRatio {
        widthConstraint.constant = min(image.size.width, screenSize.width)
        heightConstraint.constant = widthConstraint.constant / imageAspectRatio
    }
    else {
        heightConstraint.constant = min(image.size.height, screenSize.height)
        widthConstraint.constant = heightConstraint.constant * imageAspectRatio
    }
    view.layoutIfNeeded()
}

Try to check " Clip to Bounds " for imageView in Interface Builder Clip to Bounds Image View 尝试在Interface Builder Clip to Bounds Image View中检查imageView的Clip to Bounds

and you must have a well-defined height and width, not "<=" 你必须有一个明确定义的高度和宽度,而不是“<=”

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

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