简体   繁体   English

将UIImage放入UIImageView会重置UIImageView的大小

[英]Putting a UIImage in a UIImageView resets the size of the UIImageView

So I have a simple app in which the user selects media from their library and the media appears in a UIImageView. 因此,我有一个简单的应用程序,其中用户从其库中选择媒体,并且该媒体出现在UIImageView中。 This works fine, except that when the image pops up in the UIImageView , the UIImageView resizes itself to the dimensions of the image. 效果很好,只是当图像在UIImageView弹出时, UIImageView会将自身调整为图像的尺寸。 Picture for reference . 图片供参考 The storyboard file is setup like this , with the XCode suggested constraints. 故事板文件的设置是这样的 ,建议使用XCode约束。

I've tried playing around with the constraints to see if I could fix it using them, but I couldn't find a solution. 我尝试过使用约束条件,看看是否可以使用约束条件对其进行修复,但是找不到解决方案。 The only thing that sort of worked is setting the constraint equal width of the UIImage on the left-handed side to the right-handed side UIImage , but that also moved the UILabel. 唯一令之类的工作是设置约束equal width的的UIImage的左手侧到右手侧UIImage ,但也感动的UILabel。

I've tried setting the frame of the image. 我尝试设置图像的框架。

image1.frame = CGRectMake(0, 62, 300, 270);
image2.frame = CGRectMake(300, 62, 300, 270);

Any ideas? 有任何想法吗? Thanks. 谢谢。

除非您手动设置显式的宽度/高度约束,或者既设置前/后约束又设置上/下约束,否则imageView会将自身调整为其固有内容大小,在这种情况下为UIImage大小

I had the same problem once. 我曾经有过同样的问题。 I could fix it with the following constraints in the Attributes Inspector: 我可以在Attributes Inspector中使用以下约束来修复它:

View Mode: Scale To Fill 查看模式:缩放以填充

Check on Autoresize Subviews. 检查自动调整大小子视图。

I think it will automatically use this constraints when you set the size in the Size inspector. 我认为在“大小”检查器中设置大小时,它将自动使用此约束。

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

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