ScrollView 缺少约束 [英] ScrollView is missing constraints

查看:62
本文介绍了ScrollView 缺少约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift 5/Xcode 12.4

Swift 5/Xcode 12.4

我的应用程序使用 UIScrollView 缩放其子 UIImageView(通过代码填充).这部分有效,但 ScrollView 在 XIB 中一直抱怨它缺少 x/y 位置或宽度/高度的约束.

My app uses a UIScrollView to zoom its child UIImageView (which is filled through code). This part works but the ScrollView keeps complaining in XIB that it's missing constraints for the x/y position or width/height.

如您所见,ScrollView 具有顶部、前导和尾部约束,并且宽度设置为等于高度.根据我的理解,由于这些限制,它知道它的尺寸和位置,但显然这还不够.我不想将 ScrollView 垂直居中,并且由于应用程序必须同时支持 iPhone 和 iPad,我也不能只将固定的底部空间"设置为底部空间".ScrollView 的约束.

As you can see, the ScrollView has a top, leading and trailing constraint and the width is set to equal the height. From my understanding it knows its dimensions and position because of these constraints but apparently it's not enough. I do NOT want to center the ScrollView vertically and as the app has to support both iPhones and iPads, I also can't just set a fixed "bottom space to" constraint for the ScrollView.

我已经尝试将 ScrollView 的宽度设置为 ChildView 的宽度并将其水平居中,但警告仍然没有消失.

I already tried to set the ScrollView's width to the ChildView's width and center it horizontally in it but the warning still didn't go away.

我已经看到建议将 ScrollView 的内容放在另一个 UIView 中,但在我的情况下,它只是一个图像,如果我将其嵌入到视图中,则缩放会中断.

I've seen suggestions to put the ScrollView's contents inside another UIView but in my case it's only a single image and if I embed it inside a view, then the zooming breaks.

为什么 ScrollView 仍然缺少约束,我该如何修复它(不改变这个 ViewController 的整体布局)?

Why is the ScrollView still missing constraints and how do I fix it (without changing the overall layout of this ViewController)?

推荐答案

UIImageView(没有图像)没有 Intrinsic Content Size.

如果你打算通过代码设置它的宽度和高度约束,但你想摆脱 IB 布局的抱怨,给图像视图一个 Placeholder:

If you're going to be setting its Width and Height constraints via code, but you want to get rid of the IB layout complaints, give the image view a Placeholder:

编辑

如果您希望在 IB 中调整视图的大小,但要在运行时更改约束,您可以:

If you want the view(s) to be sized in IB, but you're going to change the constraints at run-time, you can either:

将约束连接到 @IBOutlet 并通过代码更改 .constant (如果需要,更改乘数更有问题,所以可能不是要走的路)...

Connect the constraint to an @IBOutlet and change the .constant via code (more problematic to change the multiplier if needed, so probably not the way to go)...

或者...

在 IB 中给出视图约束,但通过选中在构建时删除"将它们设置为占位符:

Give the view constraints in IB but set them as Placeholders by checking "Remove at build time":

这篇关于ScrollView 缺少约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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