子视图随自动布局消失

时间:2013-01-19 07:09:07

标签: objective-c uiview ios6 autolayout interface-builder

我刚刚将UIView作为子视图添加到接口构建器(基本单视图应用程序)上的主UIView。 没有设置任何约束,我的子视图就会消失。

subview's frame = (0 0; 320 0);

为什么?

如果我尝试添加一些约束,例如尾随空格,前导空格,顶部空间和底部空间要修复,我的视图仍会消失。

我该如何解决这个问题?

谢谢。

为了澄清一点,我创建了一个测试项目(单视图应用程序),并在主视图中添加了2个子视图,就像在图像中一样。我没有更改任何默认约束。 并且您可以在图像的日志中看到错误。 test project

日志:

**2013-01-19 17:16:02.435 Test[8871:c07] Unable to simultaneously satisfy constraints.
    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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x106178c0 h=--- v=--- V:[UIWindow:0x9917040(480)]>",
    "<NSLayoutConstraint:0x106159e0 UIView:0x991a5a0.bottom == UIWindow:0x9917040.bottom>",
    "<NSLayoutConstraint:0x991ab00 V:|-(518)-[BottomView:0x9919c90]   (Names: '|':UIView:0x991a5a0 )>",
    "<NSLayoutConstraint:0x10615960 V:|-(20)-[UIView:0x991a5a0]   (Names: '|':UIWindow:0x9917040 )>",
    "<NSLayoutConstraint:0x991aa80 BottomView:0x9919c90.bottom == UIView:0x991a5a0.bottom>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x991aa80 BottomView:0x9919c90.bottom == UIView:0x991a5a0.bottom>

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.**

约束: enter image description here

这也是模拟器上的结果: enter image description here

1 个答案:

答案 0 :(得分:0)

理解这些日志是一种好习惯,但如果您打算使用Autolayout,则必须阅读此内容。每个人都说这很简单,但我个人并不觉得这很简单。

Apples Programming Guide For AutoLayout

请阅读本指南,尤其是调试部分。

作为一个非常通用的规则,如果要添加视图,则需要为视图关闭autoresizingmasks(Spring和struts)。将视图添加为子视图并为其提供2或3个约束。在你的情况下,你会给它一个constaint,它应该有一个左视图或前导空格到superview为0.一个顶视图,超级视图为0,宽度为320.

EDIT;这是添加视图的示例;请注意,您不需要创建框架。约束可能有点奇怪。第一个将视图置于超视图的中心。第二个方法是宽度为200.下一个方法是垂直约束,它将视图置于底部并使其高2。

    UIView *sView = [[UIView alloc] init];
[sView setTranslatesAutoresizingMaskIntoConstraints:NO];
[superView addSubview:sView];

[superView addConstraint:[NSLayoutConstraint constraintWithItem:sView
                                                 attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:superView
                                                 attribute:NSLayoutAttributeCenterX
                                                multiplier:1.0
                                                  constant:0]];

[superView addConstraint:[NSLayoutConstraint constraintWithItem:sView
                                                             attribute:NSLayoutAttributeWidth
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:Nil
                                                             attribute:NSLayoutAttributeWidth
                                                            multiplier:1.0
                                                              constant:200]];

[superView addConstraints:[NSLayoutConstraint
                                  constraintsWithVisualFormat:@"V:[sView(2)]|"
                                  options:0
                                  metrics:nil
                                  views:NSDictionaryOfVariableBindings(sView)]];