2013-01-19 142 views
0

我刚刚添加一个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

+0

貌似高度有0点 –

+0

是的,我知道,我只是不知道为什么它被设置为0。 –

+0

你可以粘贴错误的文本,无法读取它那里。 –

回答

0

这是很好的做法,以了解这些日志,但如果你是要使用Autolayout,你将不得不阅读这个。大家都说这很简单,但我个人并没有发现它很简单。

Apples Programming Guide For AutoLayout

请阅读本指南特别是调试部分。

作为一个非常普遍的规则,如果你要添加一个视图,那么你需要关闭视图的autoresizingmasks(Spring和struts)。将该视图添加为子视图并给它2或3个约束。在你上面的情况下,你会给它一个约束,它应该有一个左或领先的空间来超视图0顶视图,0和320的宽度。以下是添加视图的示例;注意你不需要创建一个框架。这些约束可能有点奇怪。第一个将视图置于超视图的中心。第二个宽度为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)]]; 
+0

我确实添加了约束条件。 如何关闭autoresizingmasks? 我做了setAutoresizingmasks:在我的viewDidLoad NO,但它没有效果。 –

+0

将它设置在您要添加的UIView上,并在将它添加到超级视图之前将其设置, – darbid

+0

如何在UIView上设置它? 我没有继承它,它是一个普通的UIView。 并将它添加到界面构建器的超级视图中。上面的 –