随着自动布局,如何让我一个UIImageView的大小取决于动态的形象? [英] With Auto Layout, how do I make a UIImageView's size dynamic depending on the image?

查看:248
本文介绍了随着自动布局,如何让我一个UIImageView的大小取决于动态的形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的的UIImageView 来增加或取决于什么它显示的实际图像的大小缩小。但我希望它远离上海华前缘垂直居中和10pts。

I want my UIImageView to grow or shrink depending on the size of what the actual image it's displaying is. But I want it to stay vertically centered and 10pts from the leading edge of the superview.

不过,如果我设置这两个约束它抱怨我没有设置足够的约束,以满足自动布局。对我来说,似乎完美地描述了我想要的东西。我缺少什么?

However, if I set these two constraints it complains I haven't set up enough constraints to satisfy Auto Layout. To me it seems to perfectly describe what I want. What am I missing?

推荐答案

图片视图的固有尺寸已经依赖于图象的大小。你的假设(和约束是正确的)。

The image view's intrinsic size is already dependent on the size of the image. Your assumptions (and constraints are correct).

不过,如果你已经设置了图像视图界面生成器中,并没有与图像,然后布局系统(界面生成器),只要它不知道你的图像视图有多大应该是在编译时。你的布局是模糊的,因为你的图像视图,可能还有很多的大小。这就是引发错误。

However, if you've set up your image view in interface builder and have not provided it with an image, then the layout system (interface builder) won't know how big your image view is supposed to be at compile time. Your layout is ambiguous because your image view could be many sizes. This is what throws the errors.

在您设置的图像显示的图像属性,则图像视图的内在大小由图像的大小来定义。如果你设置视图在运行时,那么你可以做什么安娜提到的,提供的接口建设者在图像视图的属性检查器的占位符固有大小。这告诉界面生成器,使用这个大小现在,以后我会给你一个真实的大小。占位符限制在运行时会被忽略。

Once you set your image view's image property, then the image view's intrinsic size is defined by the size of the image. If you're setting the view at runtime, then you can do exactly what Anna mentioned and provide interface builder with a "placeholder" intrinsic size in the property inspector of the image view. This tells interface builder, "use this size for now, I'll give you a real size later". The placeholder constraints are ignored at runtime.

您另一种选择是将图像分配到直接接口生成器中的图像视图(但我相信你的图像是动态的,所以这不会为你工作)。

Your other option is to assign the image to the image view in interface builder directly (but I assume your images are dynamic, so this won't work for you).

这篇关于随着自动布局,如何让我一个UIImageView的大小取决于动态的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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