如何扩展一个UIImage以方与自动布局一个UIScrollView内 [英] How to scale a UIImage view to a square with Autolayout inside a UIScrollView

查看:178
本文介绍了如何扩展一个UIImage以方与自动布局一个UIScrollView内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力学习自动布局(最终)。我想有一个垂直滚动的UIScrollView持有该视图中的所有内容。我想要锁定一个UIImage视图滚动视图的顶部,并保持该图像一个正方形(scaleToFill)。基本上,我想这样做(我只是想和自动布局做):

I've been struggling to learn autolayout (finally). I want to have a vertically scrolling UIScrollView that holds all the content for the view. I want to pin a UIImage view to the top of the scrollview and keep the image a square (scaleToFill). Basically I want to do this (only I want to do it with autolayout):

我不能获得图像,以保持其高宽比,同时在屏幕范围内停留。每当我添加的纵横比约束的ImageView增长到600像点(PNG我想的宽度),但我希望它只是一样宽屏幕。

I can't get the image to keep its aspect ratio while staying within the screen bounds. Whenever I add an aspect ratio constraint the imageView grows to like 600 points (the width of the png I think), but I want it to just be as wide as the screen.

我想我正确地设置了限制为的UIImageView,因为如果我摆脱了滚动型而直接拖放imageViw的看法话,好像做什么,我希望它。诀窍是把它滚动视图中。

I think I am setting the constraints up for the UIImageView correctly because if I get rid of the scrollView and just drop the imageViw directly on the view then it seems to do what I want it to. The trick is putting it inside the scrollView.

这是如何我目前有它设置:

This is how I currently have it set up:

4垂直/水平空间滚动视图约束都设置为常数0

The 4 vertical/horizontal space constraints on the scroll view are all set to constant 0.

推荐答案

由于该的UIScrollView 取决于其内容的大小,你不能有你的<$大小C $ C>的UIImageView 子视图依赖于它。

Since the size of the UIScrollView depends on its content, you cannot have the size of your UIImageView subview dependent on it.

您必须删除的的UIImageView 纵横比限制,并使用固定宽度的。你也可以有你的的UIImageView 宽度依赖于另一个视图的UIScrollView 里面有一个固定的宽度或以其他方式明确宽度。

You have to remove the aspect ratio constraint of the UIImageView and use a fixed width one. You can also have your UIImageView width dependent on another view in the UIScrollView which has either a fixed width or otherwise unambiguous width.

不要忘了使用占位符在Interface Builder的内在大小值。

Don't forget to use Placeholder as the intrinsic size value in Interface Builder.

希望这有助于。

这篇关于如何扩展一个UIImage以方与自动布局一个UIScrollView内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆