简体   繁体   English

ScrollView内的iOS Stackview具有保留图像纵横比

[英]iOS Stackview inside ScrollView with preserve image aspect ratio

I'm having a problem with scrollable content. 我在滚动内容方面遇到问题。 Basically, I want to place one ImageView and one Label on the screen with ImageView width equals screen width. 基本上,我想在屏幕上放置一个ImageView和一个Label ,并且ImageView宽度等于screen宽度。 height depends on intrinsic content aspect ratio (while width is always equal screen width). height依赖于intrinsic content aspect ratio (宽度而始终等于屏幕宽度)。

The label is 20 spacing bellow ImageView . label是20间距的波纹管ImageView That means whole content height may less or equal or even longer than the root view . 这意味着整个内容高度可能小于或等于根view What I have tried is to put Label and ImageView inside a StackView ( ImageView has Aspectfit setting), Then put StackView inside ScrollView . 我尝试过的是将LabelImageView放在StackViewImageView具有Aspectfit设置),然后将StackView放在ScrollView Set up constraints as below. 设置constraints如下。

在此处输入图片说明

The problem is the distance between ImageView and top Superview and between ImageView and Label is too big (ideally should be 20). 问题是ImageView和顶部Superview之间以及ImageViewLabel之间的距离太大(理想情况下应为20)。 Is there any way to achieve the desired outcome? 有什么办法可以达到预期的结果?

PS: I have tried to set stackView distributtion but no help PS:我试图设置stackView分布,但没有帮助

在此处输入图片说明

在此处输入图片说明

tl;dr Remove the aspect ratio constraint and add a height constraint for the UIImageView . tl; dr删除纵横比约束,并为UIImageView添加高度约束。

It's important to understand that UIImageView is basically a container that holds an image, and its dimensions don't necessarily reflect the image's dimensions (unless you manually make them equal). 重要的是要了解UIImageView基本上是一个保存图像的容器,并且其尺寸不一定反映图像的尺寸(除非您手动使它们相等)。 When you selected Aspect Fit as UIImageView 's Content Mode, the size of the container didn't change to correspond the image's size; 当选择Aspect Fit作为UIImageView “内容模式”时,容器的大小不会更改为与图像的大小相对应。 this setting only changed the way the image is placed inside the container. 此设置仅更改了将图像放置在容器内的方式。

As I can see in your screenshot, the UIImageView 's height is greater than its width; 如您在屏幕快照中所见, UIImageView的高度大于其宽度; furthermore, the aspect ratio is fixed by the constraint you've added. 此外,长宽比由您添加的约束固定。 When you placed a landscape-oriented image inside this container, the latter left white bars on the top and the bottom of the image (just like the black bars you see when you watch a widescreen video on an old monitor). 当您在该容器中放置横向图像时,后者在图像的顶部和底部都留有白色条(就像在旧显示器上观看宽屏视频时看到的黑条一样)。 Change Content Mode to Aspect Fill or Scale to Fill to see the actual size of the UIImageView . 将“内容模式”更改为“长宽比填充”或将“缩放比例填充”更改为查看UIImageView的实际大小。

To fix this, remove the aspect ratio constraint and set up a fixed height for either the UIImageView or the UIStackView . 要解决此问题,请删除长宽比约束并为UIImageViewUIStackView设置固定的高度。 If I were you, I'd probably set the UIStackView 's height equal to Safe Area so that no matter how tall the stack view is, it doesn't go beyond the screen boundaries. 如果您是我,我可能UIStackView的高度设置为等于“ Safe Area以便无论堆栈视图有多高,它都不会超出屏幕边界。

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

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