简体   繁体   English

如何使用带有aspectFill的UIImageView并将其对齐到顶部?

[英]How can I use UIImageView with aspectFill and alignment to the top?

How could I use UIImageView with aspectFill and alignment to the top? 我如何使用带有aspectFill UIImageView和顶部对齐? Auto-layout or programmatically don't mind. 自动布局或以编程方式不介意。

It can be done, but you are talking about quite a bit of coding. 它可以完成,但你正在谈论相当多的编码。 Let's start with a detailed description of what you are asking for. 让我们首先详细描述您的要求。

We'll start with an iPhone6s in portrait mode with a screen size of 667x375 points, or a regular height by compressed width in size classes. 我们将从纵向模式的iPhone6s开始,屏幕尺寸为667x375点,或按尺寸类别的压缩宽度定期高度。

(Keep in mind, on any iPad you'll also need to work with slide-over and split screen too.) (请记住,在任何iPad上,您还需要使用滑动和分屏。)

But for now we'll go with 667x375. 但是现在我们将使用667x375。 Into that we'll add our imageView as a square size of 375x375. 我们将imageView添加为375x375的正方形尺寸。 Along the top of the screen when in portrait mode. 在纵向模式下沿屏幕顶部。

(Keep in mind that with AutoLayout you'll also have it pinned, and without putting some code in to determine if it's in landscape or portrait, otherwise, that 375x375 will shrink in landscape if you have other views underneath it.) (请记住,使用AutoLayout,您也可以将其固定,并且无需放入一些代码来确定它是否为横向或纵向,否则,如果您下方有其他视图,则375x375将在横向缩小。)

So now, stripped out to the most minimum (for now), we are talking about how to pin "an image" to the top of a UIImageView that is 375x375 in size and with a contentMode of aspectFill. 所以现在,剥离到最低限度(现在),我们正在谈论如何将“图像”固定到尺寸为375x375且内容模式为aspectFill的UIImageView的顶部。

We need to find the size of the image next, based on both the largest dimension (height or width) AND how that dimension compares to the size of the imageView, calculate how the imageView will normally render it. 我们需要根据最大尺寸(高度或宽度)以及该尺寸与imageView的大小进行比较来查找下一个图像的大小,计算imageView通常如何渲染它。 The key figure you want is the height it will render it as 你想要的关键人物是它将它呈现为的高度

For instance, how will a 480x1020 (HxW) image fit into it? 例如,480x1020(HxW)图像将如何适应它? The 1020 width will be taken down to 375, so the height will be 480/(1020375) or 176.4705 points. 1020宽度将降至375,因此高度将为480 /(1020375)或176.4705点。

Always do the math for both dimensions - if your imageView isn't square or if the height isn't the "bounded" dimension, you'll be off. 总是对两个维度进行数学计算 - 如果你的imageView不是正方形,或者如果高度不是“有界”维度,你就会离开。

Once you've calculated what the "Aspect Filled" height will be, check it against the dimensions of the imageView, and adjust the height of it as needed. 计算出“Aspect Filled”高度后,请根据imageView的尺寸进行检查,并根据需要调整高度。

Here's where the real work comes in. If you are using AutoLayout, you can do a heightAnchor adjustment, but depending on how you pinned those other views things may be laid out really bad after you do. 这里是实际工作的地方。如果你正在使用AutoLayout,你可以进行heightAnchor调整,但是根据你固定那些其他视图的方式,你做的事情可能会非常糟糕。 If you are using frames and CGRects be aware that you have to take into account iPad slide-out and split screen, etc. 如果你正在使用框架和CGRects意识到你必须考虑到iPad滑出和分屏等。

Finally, if you want your app to load a different image in that imageView after it was loaded, you will have to "reset" the height back to what it originally was, at least in your calculations. 最后,如果您希望应用在加载后在该imageView中加载不同的图像,则必须将高度“重置”回原来的高度,至少在计算中如此。

My recommendation is to find an alternative to what you are asking. 我的建议是找到你所要求的替代品。 For instance, put a small border around the image view to let the user know the true dimensions of that view. 例如,在图像视图周围放置一个小边框,让用户知道该视图的真实尺寸。

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

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