自动布局(约束)在父视图中居中2个并排视图 [英] Auto Layout (Constraints) Center 2 side by side views in a parent view
问题描述
我想弄清楚如何做到这一点与汽车布局(iOS6)和约束。
基本上我有我的大视图分为两个部分底端。在这些部分(当前子视图)的内部,我有一个图像视图和标签。我想把它们放在两边,用可变长度的文字。
我的头大部分是围绕汽车布局,但我不确定最好的方法。我倾向于认为它不可能在IB,但在代码中。
继续尝试找出这一点,但在此期间,这里是例子我
这是你以后吗?
img src =https://i.stack.imgur.com/GXRcD.pngalt =短标签>
我在<$ c $中添加了一个视图(名为 viewCenteredInLeftSection
c> leftSection ,然后将时钟图片和标签添加为具有以下约束的子视图:
- $ c> viewCenteredInLeftSection 的CenterX和CenterY等于其超级视图(
leftSection
)。 - code> clockImage 的顶部,底部和前沿等于其超级视图(
viewCenteredInLeftSection
)。 - 使
标签
的尾随边缘等于其超级视图(viewCenteredInLeftSection
)。 - 让
clockImage
的后沿距离标签
的前沿。 li>
我在Interface Builder中调整iOS UIView的大小时遇到问题,所以我创建了我的OS X示例,我可以在Interface Builder中完成。如果你在Interface Builder中制作上述约束有困难,让我知道,我会发布代码来创建它们。
2014-08- 26编辑
。我突出显示了标签和 viewCenteredInLeftSection
,然后使用对齐>尾随边缘应用约束。
更容易创建Xcode 5的界面生成器。 pngalt =Interface Builder>
I'm trying to figure out how to do this with auto layout (iOS6) and constraints.
Basically I have my large view divided up into two sections on the bottom. Inside of those sections (currently subviews) I have an image view and a label. I want to center those on both sides, with variable length text.
My head is mostly wrapped around auto layout, but I'm not sure the best approach to this. I'm inclined to think it's not possible in IB, but is in code.
Going to continue trying to figure this out, but in the meantime here is the example I'm trying to create.
Is this what you're after?
I did it by adding a view (named viewCenteredInLeftSection
) within your leftSection
, then adding the clock image and label as subviews with these constraints:
- Make
viewCenteredInLeftSection
's CenterX and CenterY equal to its superview's (leftSection
). - Make
clockImage
's Top, Bottom, and Leading edges equal to its superview's (viewCenteredInLeftSection
). - Make
label
's Trailing edge equal to its superview's (viewCenteredInLeftSection
). - Make
clockImage
's Trailing edge the standard distance away fromlabel
's Leading edge.
I have trouble resizing iOS UIViews in Interface Builder, so I made my example for OS X, and I was able to do so entirely in Interface Builder. If you have trouble making the above constraints in Interface Builder, let me know, and I'll post code that'll create them.
2014-08-26 Edit
Luda, here are Xcode 5's Pin and Align menus, also available in Xcode's menu bar:
Below is what my example looks like in Interface Builder. The blue view is the "parent view" from the original question, the given view in which the image and label should be centered.
I added the green view (which I named viewCenteredInLeftSection
) as a subview of "parent view". Then I highlighted it and used the Align menus "Horizontal Center in Container" and "Vertical Center in Container" to create constraints to define its position.
I added the clock image as a subview of viewCenteredInLeftSection
, with constraints defining its width and height. I highlighted the clock image and viewCenteredInLeftSection
, then applied constraints using Align > Leading Edges, Align > Top Edges, and Align > Bottom Edges.
I added the label as a subview of viewCenteredInLeftSection
, positioning it to be the standard Aqua space distance from the clock image. I highlighted the label and viewCenteredInLeftSection
, then applied constraints using Align > Trailing Edges.
This was much easier to create with Xcode 5's Interface Builder versus Xcode 4's.
这篇关于自动布局(约束)在父视图中居中2个并排视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!