iPad上的奇怪约束行为 [英] Strange constraints behaviour on iPad

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

问题描述

我尝试做一个简单的事情,得到一个奇怪的结果。

I try to do one of the simples things ever and get a strange result.

我有一个UIViewController,其中有一个UIImageView

I have a UIViewController with one UIImageView inside

我设置的限制如下

我得到以下结果

>

我的问题是


  1. 为什么我在左侧和右侧获得填充要删除该填充,我必须使用从-20而不是-16的约束,然后是框架x - 4.

  1. Why I get a padding on the left and right side? To remove that padding I have to use a constrains from -20 instead of -16 but then is the frame x - 4.

为什么垂直空间

以前有人有这种行为,现在该如何解决?

Does someone has this behaviour before and now how to solve it?

https://github.com/eikebartels/iPadTest
https://github.com/eikebartels/iPadTest.git

提前感谢,

Eike

推荐答案

问题是你已经将你的约束连接到超级视图的边距。如果你想到达超级视图的边缘,你需要连接到超级本身(常数为零)。

The problem is that you've connected your constraints to the superview's margins. If you want to reach the edge of the superview you need to connect to the superview itself (with a constant of zero).

注意约束说明中的单词margin:

Note the word "margin" in the constraint description:

连接到边距的问题是,您不知道应用程序运行时的边距。这是一个危险的策略。但你知道视图的边缘在哪里,所以如果你想匹配他们,约束到他们,而不是边距。只有当你真的想要匹配边距时,使用边距,无论它们是什么。例如,具有0到常数的常数的约束保证了超级视图的边缘的良好空间。但你不想要任何空间!

The problem with connecting to the margins is that you do not know what the margins will be when the app runs. It is a risky strategy. But you do know where the edges of the view will be, so if you want to match them, constrain to them, not to the margins. Use the margins only if you really want to match the margins, whatever they may be. For example, a constraint with a constant of 0 to a margin guarantees a nice space to the edge of the superview. But you do not want any space!

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

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