如何在 Xcode Interface Builder 中设置约束,以便视图相应地更改其宽度或高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26306481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set in Xcode Interface Builder constraints so that views change their width or height accordingly?
提问by Uriel Arvizu
I'm using Xamarin Studio for iOS development and I've just started to use the Xcode Interface Builder and it is giving me headaches. My Xcode version is 5.1.1(5B1008) and I'm on Xamarin Studio 5.5 build 227 with Xamarin.iOS 8.2.0.193 on Starter Edition.
我正在使用 Xamarin Studio 进行 iOS 开发,而且我刚刚开始使用 Xcode Interface Builder,这让我很头疼。我的 Xcode 版本是 5.1.1(5B1008),我在 Xamarin Studio 5.5 build 227 上使用 Xamarin.iOS 8.2.0.193 入门版。
I'm not able to understand how to use the Auto-Layout functionality and I've read tutorials, most say the say, drag element into view, click on Pin icon, set constraints and be done with it.
我无法理解如何使用自动布局功能,我已经阅读了教程,大多数人说,将元素拖到视图中,单击固定图标,设置约束并完成它。
But the elements I add don't end up looking as I want them to be.
但是我添加的元素最终看起来并不像我想要的那样。
For example let's say I want to create a view with a scroll view in it and that will display labels and text fields in it as if it were a form, by dragging-dropping element imagine I end up with something like this:
例如,假设我想创建一个带有滚动视图的视图,它将在其中显示标签和文本字段,就好像它是一个表单一样,通过拖放元素想象我最终得到这样的东西:
The layout I desire is for every label and text field to be 11 dps apart from the left and right, no matter the orientation, also each needs to be 11 dps apart from each other, with the initial label to be 50 dps apart from the top of the scroll view.
我想要的布局是每个标签和文本字段的左右距离为 11 dps,无论方向如何,也需要彼此相距 11 dps,初始标签为 50 dps。滚动视图的顶部。
Every element should be the same height (20 dps) always but their width must change according to the left and right constraints.
每个元素都应该始终具有相同的高度(20 dps),但它们的宽度必须根据左右约束而改变。
That's the end of my layout definition.
我的布局定义到此结束。
Now let's start with defining the left and right constraints along with the height. What I do is to select all of the label and textfields, I pin the left and right and set their height to the same value:
现在让我们开始定义左右约束以及高度。我要做的是选择所有标签和文本字段,固定左右并将它们的高度设置为相同的值:
But what I get looks like this:
但我得到的看起来是这样的:
The resulting layout looks nothing like I wanted, the width for every element is set to the same size and the right values don't make sense, I stated 11 dps but I get values like 187, 173 and 228.
结果布局看起来不像我想要的,每个元素的宽度都设置为相同的大小,正确的值没有意义,我说 11 dps 但我得到了 187、173 和 228 之类的值。
Even if I set the top and bottom constraints, the right constraints keep looking the same:
即使我设置了顶部和底部约束,正确的约束看起来仍然相同:
How do I fix this? I've deleted and started from scratch several times but I can't find to understand what am I doing wrong. Some of the technical language challenges me sometimes (I'm not a native english speaker) so I don't know if I'm interpreting something differently.
我该如何解决?我已经多次删除并从头开始,但我无法理解我做错了什么。有些技术语言有时会挑战我(我的母语不是英语),所以我不知道我是否在解释不同的东西。
Some of the links for the tutorials I've looked into are this, thisand this.
回答by rob mayoff
You need to ask Interface Builder to update the frames of those elements. All those yellow lines/numbers are constraints that are currently not satisfied - the frames of the elements don't match what the constraints require.
您需要要求 Interface Builder 更新这些元素的框架。所有这些黄线/数字都是当前不满足的约束 - 元素的框架与约束要求不匹配。
However, it's often better to make sure you have all your constraints set up first, before asking IB to update the frames.
但是,在要求 IB 更新框架之前,最好先确保您设置了所有约束。
Here, I've set up all the constraints you described, but I haven't asked IB to update the frames:
在这里,我已经设置了您描述的所有约束,但我没有要求 IB 更新框架:
Here, I've asked IB to update the frames of all views in the top-level view:
在这里,我已经要求 IB 更新顶级视图中所有视图的框架:
If I select the subviews, I can see that the constraints are now shown in blue, meaning they are satisfied:
如果我选择子视图,我可以看到约束现在显示为蓝色,这意味着它们得到满足: