带有约束的iOS 6.0 AutoLayout问题:子视图在旋转时消失

时间:2013-02-20 20:26:16

标签: xcode ios6 interface-builder xib autolayout

您好我设计了一个使用AutoLayout constriants的XIB来显示两行按钮,每行有三个按钮,如屏幕截图所示(在获得满意的布局后,我会将按钮放在Red UIViews中):

Portrait Mode Autolayout

到目前为止一直很好,在如上图所示的纵向模式下按钮看起来很好。然而,当我旋转到横向时,标题(蓝色)和父视图(灰色)调整大小并正确显示,但红色按钮视图刚刚消失(见下文)。

enter image description here

设置约束以使其正常工作的最佳方法是什么?我已经按照Ray Wenderlich教程使用了他在他的示例中使用的相同Editor \ Pin] Vertical Spacing和Editor \ Pin \ Horizo​​ntal间距:        Ray Wenderlich tutorial

我也遇到了很多约束错误,如下所示:

        Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x859abe0 UIView:0x85a8fb0.height == UIView:0x85a8e60.height>",
    "<NSLayoutConstraint:0x859ab60 V:[UIView:0x85a8fb0]-(114)-|   (Names: '|':UIView:0x85a8170 )>",
    "<NSLayoutConstraint:0x859ab20 V:|-(424)-[UIView:0x85a8fb0]   (Names: '|':UIView:0x85a8170 )>",
    "<NSLayoutConstraint:0x85a3430 V:|-(150)-[UIView:0x85a8170]   (Names: '|':UIView:0x85a8da0 )>",
    "<NSLayoutConstraint:0x85a33b0 V:[UIView:0x85a8170]-(NSSpace(20))-|   (Names: '|':UIView:0x85a8da0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x75ac610 h=--& v=--& V:[UIView:0x85a8da0(704)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x859ab60 V:[UIView:0x85a8fb0]-(114)-|   (Names: '|':UIView:0x85a8170 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

我的斗智尽头,所以这个博学群体的任何帮助都会非常感激......!

1 个答案:

答案 0 :(得分:2)

错误意味着您有相互冲突的约束。试着让它在一行工作。

  1. 将每个红色框与蓝色标题框的垂直间距固定
  2. 将带有前导空格的左侧红色框固定到superview
  3. 将带有尾随空格的右侧红框固定到superview
  4. 将水平间距设置为中间红框的左侧和右侧
  5. 修复身高
  6. 选择所有三个红色框并将它们设置为具有相等的宽度(宽度相等)
  7. 删除IB添加的任何无关限制
  8. 在设备上测试。 (我在界面构建器中模拟了横向/纵向)有时会更改约束。 9它应该看起来像附加图像enter image description here