简体   繁体   中英

Subview disappears with auto layout

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);

Why is that?

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.

How can I solve this?

Thank you.

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. 测试项目

Logs:

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

Apples Programming Guide For AutoLayout

Please read this guide especially the debugging section.

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.

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)]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM