AutoLayout - 保持图像的比例与水平约束(Swift Xcode 6)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27119325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:15:05  来源:igfitidea点击:

AutoLayout - Keep Ratio of Image With Horizontal Constraints (Swift Xcode 6)

iosxcodeswiftuiimageviewautolayout

提问by fabdarice

I've been trying to implement a timeline of picture (as instagram or facebook). However, I've been struggling with the ratio of the image.

我一直在尝试实现图片时间线(如 instagram 或 facebook)。但是,我一直在努力解决图像的比例问题。

Here's my xib that contains my Cell : enter image description here

这是包含我的 Cell 的 xib : 在此处输入图片说明

I want the picture to keep its ratio and to completely fill the width of the cell. I've set 4 autolayout constraint on the UIImageView as 0-0 for horizontal constraint and 5-5 for top/bottom constraints. I've also set the ViewMode to "Aspect Fit".

我希望图片保持其比例并完全填充单元格的宽度。我在 UIImageView 上设置了 4 个自动布局约束,水平约束为 0-0,顶部/底部约束为 5-5。我还将 ViewMode 设置为“Aspect Fit”。

I've tried different setup :

我尝试过不同的设置:

  1. set the height of UIImageView to a specific value : --> The width of the image is modify to keep the image ratio
  2. don't set the height of UIImageView --> it considers that the height is equal to 0 and therefore doesn't even display the image
  3. set the height greater or equal to a specific value : -- > same behavior as 1. (The width of the image is modify to keep the image ratio)
  1. 将 UIImageView 的高度设置为特定值 : --> 修改图像的宽度以保持图像比例
  2. 不要设置UIImageView的高度-->它认为高度等于0,因此甚至不显示图像
  3. 将高度设置为大于或等于特定值:--> 与 1 相同的行为。(修改图像的宽度以保持图像比例)

I'm loading the image from an URL so I've been using the lib Haneke (https://github.com/Haneke/HanekeSwift) with the following function : imageView.hnk_setImageFromURL(url).

我正在从 URL 加载图像,所以我一直在使用 lib Haneke ( https://github.com/Haneke/HanekeSwift) 和以下函数:imageView.hnk_setImageFromURL(url)。

It has an automatic resize of the image and I don't know how to get the original width/height of the image to set the height manually.

它具有图像的自动调整大小,我不知道如何获取图像的原始宽度/高度以手动设置高度。

Again, what I want is the picture to keep its ratio and to completely fill the width of the cell.

同样,我想要的是图片保持其比例并完全填充单元格的宽度。

Anyone has a suggestion ?

有人有建议吗?

Thanks a lot !

非常感谢 !

回答by Sean

This is the setup I came up with. It's a quick and dirty demo, but I would assume you'd want the image to always be centered. You can set the Horizontal and Vertical Center Constraint in storyboard to always keep it centered If you want it to always completely fill the view, you can replace Equal Heightwith Equal Width, but by doing so, the imageView will run off the screen on some sizes because of the aspect ratio constraint

这是我想出的设置。这是一个快速而肮脏的演示,但我假设您希望图像始终居中。您可以在故事板设置的水平和垂直中心约束始终居中如果你想让它总是完全填充视图,可以替换Equal Height使用Equal Width,但这样做,ImageView的将上因为某些尺寸跑出画面纵横比约束

Autolayout Setup

自动布局设置

I've also included screenshots of the view at different sizes

我还包括了不同尺寸的视图截图

Any

任何

Portrait

肖像

Landscape

风景