在界面构建器中使用约束 [英] Working with constraints in interface builder

查看:118
本文介绍了在界面构建器中使用约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我无法理解界面构建器中的约束。

So, I can't get my head around constraints in interface builder.

我自己无法添加它们 - 界面构建器似乎认为它最了解。

I can't add them myself - interface builder seems to think that it knows best.

我也不能把它们带走(我可以转换为用户约束,然后删除,但是界面构建器只会用相同的约束替换它)。

I also can't take them away (I can convert to a "user" constraint, then delete, but interface builder will just replace it with the same constraint again).

我认为约束应该可以帮助你改变布局和所有种类 - 即建立iPhone 5和早期版本的视图。此外,从纵向模式转为横向模式。

I thought constraints were supposed to help you with changing layouts and all sorts - i.e. building views for iPhone 5 and earlier ones. Also, moving from portrait mode to landscape mode.

我似乎根本无法使用它。如果我添加一个视图并将其放在superview的顶部下方,那么它会将顶部空间设置为:约束。这将是一个像素值,这意味着当我在较短的iPhone 4上观看时(或如果手机转动风景),这些元素不会自动向上移动。

I can't seem to get it to work at all. If I add a view and I place it below the top of the superview then it will set a Top space to: constraint. This will be a pixel value which means that when I view on the shorter iPhone 4 (or if the phone turns landscape) these elements don't auto move up.

For例如,我正在尝试创建一个自定义键盘视图 - 类似于您在收银机上看到的视图。我已经创建了一个自定义的UIView子类,现在我已经将UIView放入了界面构建器中。我设置了高度和宽度,并将其定位在视图的底部(点击到位的位置)。界面构建器如何将顶部空间添加到约束,将其置于该高度(iphone 5)。因此,如果我在iPhone 4上观看它并没有预料到(正如我所料,因为我将它啪地移动到屏幕底部)但是切断了底部。

For instance, I am trying to create a custom keyboard view - one similar to that you would see on a cash register. I have created a custom UIView subclass and now I have put a UIView into interface builder. I have set the height and width and positioned it at the bottom of the view (where it "clicked" into place). How interface builder has added a Top space to constraint putting it at that height (iphone 5). So if I view it on the iPhone 4 it doesn't budge up (as I would expect, as I "snapped" it into place at the bottom of the screen) but instead cuts off the bottom.

这不是我想要的,但是(如上所述)我不能不幸删除约束。

This isn't what I wanted, but (as above) I can't delete the constraint unfortunately.

我该怎么办?我知道我可以创建两个nib文件(适用于iPhone 5和其他iphone)但是当我转动手机景观时我想要自动预测的内容呢?

What can I do? I am aware that I could just create two nib files (for iPhone 5 and for other iphones) but what about when I want things to auto-budge when I turn the phone landscape?

感谢您的帮助

推荐答案

如果这样做会离开,则无法在Interface Builder中删除约束没有足够的约束来确定完全任何界面小部件的大小和位置

You can't remove a constraint in Interface Builder if doing so would leave insufficient constraints to determine completely the size and position of any interface widget.


  • 一种解决方案:添加更多约束,直到大小和位置超定。然后,您将能够删除其中一个多余的约束。例如,你给出了这个例子:

  • One solution: add more constraints, until the size and position are overdetermined. You will then be able to delete one of the superfluous constraints. For instance, you gave this example:


我设置了高度和宽度,并将其定位在
视图的底部(它点击到位)。界面构建器如何添加
一个顶部空间来约束将它放在那个高度(iphone 5)。因此,如果
我在iPhone 4上查看它并没有让步(正如我所预期的那样,因为我将b $ b点击到屏幕底部的位置)而是削减了
从底部开始。

I have set the height and width and positioned it at the bottom of the view (where it "clicked" into place). How interface builder has added a Top space to constraint putting it at that height (iphone 5). So if I view it on the iPhone 4 it doesn't budge up (as I would expect, as I "snapped" it into place at the bottom of the screen) but instead cuts off the bottom.

选择视图并选择编辑器>固定>底部空间到超级视图。现在你有比你需要的更多的约束:高度,宽度,确定水平位置的东西(你忘了提到),以及超视图顶部的一个引脚和超视图底部的一个引脚。因此,您可以现在将引脚删除到顶部。

Select the view and choose Editor > Pin > Bottom Space to Superview. Now you have more constraints than you need: the height, the width, something determining the horizontal position (which you forgot to mention), and a pin to the top of the superview and a pin to the bottom of the superview. Thus you can now delete the pin to the top.

另一种解决方案:删除代码中不需要的约束。约束是一个普通的接口对象,因此您可以创建一个插座并在代码运行时将其删除。但是,如果你这样做会使尺寸或位置不确定,你会得到不可预测的结果(模糊布局),所以要小心。

Another solution: remove the unwanted constraints in code. A constraint is an ordinary interface object, so you can make an outlet to it and remove it when your code runs. However, if you do this in such a way as to leave the size or position underdetermined, you will get unpredictable results ("ambiguous layout"), so be careful.

还有另一种解决方案:降低IB中不需要的约束的优先级。 Interface Builder将确保绝对确定所有值。但也许您知道,例如,superview将改变大小,因此Interface Builder创建的一些约束将是不合需要的。您无法删除它们,但可以降低其优先级。因此,在您的示例中,最终可能会有一个顶部的引脚和一个底部的引脚,但您可以将引脚的优先级降低到顶部。

Still another solution: reduce the priority of unwanted constraints in IB. Interface Builder will ensure that all values are absolutely determined. But perhaps you know, for example, that the superview will change size and so some of the constraints created by Interface Builder will be undesirable. You can't delete them, but you can reduce their priority. Thus, in your example, you might end up with a pin to the top and a pin to the bottom, but you could then just reduce the priority of the pin to the top.

这篇关于在界面构建器中使用约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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