Xcode自动布局:无法调整窗口大小

时间:2013-07-24 10:30:21

标签: xcode constraints autolayout

我想要实现的目标:

Custom View 1
   aligned at the top
   fixed height = 20px
   width = window width

Horizontal Split View
   just below the custom view
   width = window width
   height = as large as possible

Custom View 2
   aligned at the bottom
   fixed height = 20px
   width = window width

这是一个非常简单的布局:窗口顶部和底部的页眉和页脚,以及两者之间的分割视图,左侧和右侧的内容应该随窗口调整大小。但是,Apple设计的Auto Layout管理器很差,我似乎无法让它工作。 (我现在摸索了大约三个小时!)

问题是:一旦我给自定义视图一个固定的高度,窗口高度被锁定,我不能再垂直调整它。我试图通过以下约束来做到这一点:

Custom View 1
    Constraints
    height = 20px

Custom View 2
    height = 20px

Constraints
    Leading horiz space from Custom View 1 to Superview = 0
    Leading horiz space from Custom View 2 to Superview = 0
    Leading horiz space from Split View to Superview = 0
    Trailing horiz space from Custom View 1 to Superview = 0
    Trailing horiz space from Custom View 2 to Superview = 0
    Trailing horiz space from Split View to Superview = 0

    Top vert space between Custom View 1 and Superview = 0
    Vert space between Custom View 1 and Split View = 0
    Vert space between Custom View 2 and Split View = 0
    Bottom vert space between Custom View 2 and Superview = 0
    Top vert space between Custom View 2 and Superview = 344px (!)

最后一行由Xcode添加,我无法删除它。当然,这是固定垂直布局的原因。我怎么能摆脱它?

2 个答案:

答案 0 :(得分:2)

好的,我终于明白了。

我不得不留下IB的冗余约束,但优先级为1. IB给它优先级1000,总是超越我的约束。

答案 1 :(得分:0)

每个视图都需要有足够的约束来布局系统来计算位置(x和y)和大小(宽度和高度)。如果系统没有足够的信息来计算这些属性,则布局被视为不明确。 Xcode中的Interface Builder不会让您创建模糊的布局,因此它会添加约束以确保完全约束的布局。

根据您的信息,似乎subview2受到了足够的约束。但是,我想您可能没有将视图的高度设置为约束。布局系统将忽略您为视图设置的框架。这会引起歧义。 尝试向subview2添加高度约束。