子视图,支持自动布局消失 [英] Subview disappears with auto layout
问题描述
我只是增加了一个UIView作为子视图在界面生成器(基本单一视图的应用程序)的主要的UIView。
如果不设置任何限制,我的子视图中消失。
I just added a UIView as subview to the main UIView on interface builder (basic single view application). Without setting any constraints, my subview disappears.
subview's frame = (0 0; 320 0);
这是为什么?
如果我尝试添加一些约束像尾随的空间,领先的空间,顶部空间和底部空间是固定的,还是我的视野中消失。
If I try to add some constraints like for trailing space, leading space, top space and bottom space to be fixed, still my view disappears.
我该如何解决呢?
感谢您。
我只想澄清一点一点我创建了一个测试项目(单个视图应用程序),并增加了2子视图到主视图类的形象。我没有改变任何默认的约束。
你可以看到日志的图像错误。
Just to clarify thing a little I created a test project (single view application), and added 2 subview to the main view like in the image. I didn't change any default constraint. And you can see the error in the log of the image.
日志:
**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.**
约束:
Constraints:
另外这里是在模拟器上的结果:
Also here is the result on the simulator:
推荐答案
这是很好的做法,以了解这些日志,但如果你要使用自动布局你将不得不在这读了。大家说,这是简单,但我个人还没有找到它的简单。
It is good practice to understand those logs but if you are going to use Autolayout you are going to have to read up on this. Everyone says it is simply but I personally have not found it simple.
<一个href=\"https://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/Introduction.html\"相对=nofollow>苹果编程指南自动版式
请阅读本指南特别是调试部分。
Please read this guide especially the debugging section.
至于如果你要添加视图,那么你需要关闭autoresizingmasks(春季和Struts)的视图一个非常非常一般规则。添加视图作为一个子视图,并给它2或3的约束。在你的情况比你想给它一个constaint,它应该有一个向左或导致空间的0至0上海华和320宽度的顶部空间的SuperView。
As a very very general rule if you are going to add a view then you need to turn off autoresizingmasks (Spring and struts) for the view. Add the view as a subview and give it 2 or 3 constraints. In your case above you would give it a constaint that it should have a left or leading space to superview of 0. A top space to superview of 0 and a width of 320.
编辑;此处是添加视图的例子;注意,您并不需要创建一个框架。约束可能会有点陌生。第一把在上海华的中心的图。第二给它的200的宽度的下一个方法是垂直约束这使所述视图在底部,并使其2高
EDIT; Here is an example of adding a view; note you do not need to create a frame. The constraints may be a little strange. The first puts the view in the centre of the superview. The second gives it a width of 200. The next method is the vertical constraint which puts the view at the bottom and makes it 2 high.
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)]];
这篇关于子视图,支持自动布局消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!