Xcode 11添加新的约束 set zero: 使用 set value 而不是 default/standard

我过去常常使用自动布局添加新的约束来制作像下面这样的简单约束到管理视图/相对视图:

enter image description here

然而,在最近更新到最新的 xcode (11.3.3或者仅仅是11.3/11C29)之后,我遇到了一个奇怪的问题: 当我设置常量 = 0时,它们总是以一种不想要的方式生成约束,比如:

enter image description here

看起来默认值是20,这让我的视图看起来大错特错(0比20?)

如果我像这样编辑约束来解决这个问题:

enter image description here

然后它将再次修正(图像是在编辑约束之后,在编辑之前显示常量 = Standard)

我甚至不知道如何描述这个问题,但我如何修复这个新的 add new constraint使用我的愿望点,而不是标准/默认?

更新:

这似乎只发生在对监视的约束时。不知道如何解决这个问题。

2020年4月1日更新:

Xcode11.4修复了这个。

8949 次浏览

Seems like a bug (don't forget to file with Apple). Here's a simple workaround:

When you fill in the number in the "popover", instead of typing 0, type 0.01. This will prevent the number from changing mysteriously to "Standard". Okay, so 0.01 is not the same as 0 but it's close enough that you can't tell the difference, and at least you don't have to go back and change it later.

EDIT Apple says that this bug will be fixed in Xcode 11.4:

Fixed a bug that prevented entering a 0 constant in the constraint popup editors. (54076090)

While @matt workaround is working, I just want to add on that you can set it to 0.01 and then change it to 0, it won't change to the standard value again. Note: Xcode 11.3

This behavior seems to be fixed on Xcode 11.4: release notes

Fixed a bug that prevented entering a 0 constant in the constraint popup editors. (54076090)


Original Answer:

I just found this behavior too, I tried adding the constraints 1 by 1 and Xcode actually respected the 0 value.

My two cents.

There's actually no way to solve this issue at the mean time

but the best temporary solution is to add the constraints with zero one by one .

I know that this is no solution, but for temporary solution to prevent you to go through every constraint, I just found out that you get the desired behaviour if you insert '-0' instead of '0', but you have to insert one constraint each time.

ignore that when standard.

after add it click on constrain (blue line) and inspector to constrain attribute and set it to 0.

that work for me

As of Xcode 11.3.1:

The neatest and fastest solution I've found is as follows:

Simply type in -0 into the constraint field. Xcode appears to discard the negative and it behaves correctly, which is better than it reading 0.01 for everything.

You can insert all constraints at once, without having to do one at a time or go and edit later.

Xcode displays the values as this

It does seem like really dumb behaviour. Is there a reason that Apple might have made it do this deliberately...?

Edit: This doesn't seem to work every time, which is frustrating. I've just had a UIImageView show 0 to Superview, yet still visually be at the default value (20). This really does seem like a bug with the IB as the behaviour is totally illogical.

Edit 2: Seems to be fixed now - phew!

Still experiencing this bug (Xcode 11.3.1). Fixed it by simply adding the constraints as "standard", then clicking on the blue constraint lines and setting the "constant" under size inspector to 0.