AutoLayout:UIImageView 和 Aspect Fit 内容模式
AutoLayout: UIImageView and Aspect Fit content mode
我正在尝试将 UIImageView
放在父视图的中心。图像必须保持其原始纵横比,并且不应超过父级的边界。因此横向图像应受父级宽度的限制,纵向图像应根据需要占据尽可能多的垂直 space 以保持原始比例。
AutoLayout 听起来很简单,对吧?这是我对 UIImageView
的设置:
- 在父项中垂直居中
- 在父项中水平居中
- imageView.width <= superview.width
- imageView.height <= superview.height
我也将 contentMode
设置为 Aspect Fit
。对于小于设备屏幕的小图像,一切都非常好,但由于某些原因,我的 UIImageView
比大图像的基础 UIImage
需要更多 space(注意绿色背景 - imageView.backgroundColor = [UIColor greenColor]
).
为什么会这样?我不是 AutoLayout 专家,因为我的限制在我看来是合理的。如果我使用较小的图像,如 200x400,那么 UIImageView
在屏幕上正好占据 200x400 点,没有多余的绿色区域。
我想添加边框和圆角,在这种情况下显然无法正常工作。
发生这种情况是因为您将宽度和高度限制设置为 <=。
对于小图像,imageView 的框架计算没有任何问题,并且满足所有约束。但是当设置大图的时候,imageView的size会被设置为适合图片,但同时会受到superview的尺寸(屏幕尺寸)的限制。
- 如果你确定宽高比,你可以将它设置为一个约束,
你不会看到任何绿色背景。
- 否则就留下你的
按原样设置约束并将背景颜色设置为清除颜色。这个
任何未占用的区域都是透明的,您设置的任何图像
将在至少一个维度上取最大值 space。
编辑:
可能不是最好的解决方案,有点老派。您可以添加严格的宽度和高度限制并使用图像的 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()
}
尝试在 Interface Builder 中为 imageView 检查“Clip to Bounds”
Clip to Bounds Image View
并且您必须有明确定义的高度和宽度,而不是 "<="
我正在尝试将 UIImageView
放在父视图的中心。图像必须保持其原始纵横比,并且不应超过父级的边界。因此横向图像应受父级宽度的限制,纵向图像应根据需要占据尽可能多的垂直 space 以保持原始比例。
AutoLayout 听起来很简单,对吧?这是我对 UIImageView
的设置:
- 在父项中垂直居中
- 在父项中水平居中
- imageView.width <= superview.width
- imageView.height <= superview.height
我也将 contentMode
设置为 Aspect Fit
。对于小于设备屏幕的小图像,一切都非常好,但由于某些原因,我的 UIImageView
比大图像的基础 UIImage
需要更多 space(注意绿色背景 - imageView.backgroundColor = [UIColor greenColor]
).
为什么会这样?我不是 AutoLayout 专家,因为我的限制在我看来是合理的。如果我使用较小的图像,如 200x400,那么 UIImageView
在屏幕上正好占据 200x400 点,没有多余的绿色区域。
我想添加边框和圆角,在这种情况下显然无法正常工作。
发生这种情况是因为您将宽度和高度限制设置为 <=。 对于小图像,imageView 的框架计算没有任何问题,并且满足所有约束。但是当设置大图的时候,imageView的size会被设置为适合图片,但同时会受到superview的尺寸(屏幕尺寸)的限制。
- 如果你确定宽高比,你可以将它设置为一个约束, 你不会看到任何绿色背景。
- 否则就留下你的 按原样设置约束并将背景颜色设置为清除颜色。这个 任何未占用的区域都是透明的,您设置的任何图像 将在至少一个维度上取最大值 space。
编辑:
可能不是最好的解决方案,有点老派。您可以添加严格的宽度和高度限制并使用图像的 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()
}
尝试在 Interface Builder 中为 imageView 检查“Clip to Bounds” Clip to Bounds Image View
并且您必须有明确定义的高度和宽度,而不是 "<="