首页 > 解决方案 > 在 XCode 中添加约束使 UIImageView 占据整个屏幕

问题描述

添加约束以水平对齐到中心后,如何防止 UIImageView 调整大小?每次我运行带有约束的程序时,最初大小为 300x300 的图像都会占据整个屏幕

标签: iosswiftxcode

解决方案


As soon as an image view comes under control of auto layout, its size is determined by its image — unless you take measures to prevent that. For example, you can add width and height constraints, or you can add external top-and-bottom and leading-and-trailing constraints that set the size relative to the superview or whatever.

(By the way, the very first thing you should always do is examine your interface in the View Debugger. If you really added only a constraint to align horizontally to the center, that is an error — an insufficient constraint — and the View Debugger will let you know of that fact.)


推荐阅读