简体   繁体   English

向UITableView添加约束时自动布局崩溃

[英]Auto Layout Crashes When Adding Constraints to UITableView

I am trying to add a UIView subview to a UITableView that will rotate appropriately with the device. 我试图将UIView子视图添加到将随设备适当旋转的UITableView中。 I want the subview to cover the entire table. 我希望子视图覆盖整个表格。 Here is my method, called from viewDidLoad: 这是我的方法,从viewDidLoad调用:

-(void)welcomeScreen {
    UIView *newView = [[UIView alloc] initWithFrame:self.view.frame];
    newView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];

    newView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:newView];

    NSDictionary *views = NSDictionaryOfVariableBindings(newView);

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[newView]|"
                                                                          options:0
                                                                          metrics:nil
                                                                            views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[newView]|"
                                                                          options:0
                                                                          metrics:nil
                                                                            views:views]];
}

When I run this method in an otherwise unedited project from the Master/Detail Xcode template, I get the following errors immediately on launch: 当我在Master / Detail Xcode模板的其他未经编辑的项目中运行此方法时,在启动时立即出现以下错误:

2014-08-21 13:51:02.141 TEST AutoLayout[2318:60b] *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794
2014-08-21 13:51:02.145 TEST AutoLayout[2318:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'

And here is the full stack trace: 这是完整的堆栈跟踪:

*** First throw call stack:
(
    0   CoreFoundation                      0x017f01e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0156f8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x017f0048 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x0114f4de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x0029ea38 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 567
    5   libobjc.A.dylib                     0x0158182b -[NSObject performSelector:withObject:] + 70
    6   QuartzCore                          0x03c5b45a -[CALayer layoutSublayers] + 148
    7   QuartzCore                          0x03c4f244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    8   QuartzCore                          0x03c5b3a5 -[CALayer layoutIfNeeded] + 160
    9   UIKit                               0x00360ae3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
    10  UIKit                               0x00276aa7 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
    11  UIKit                               0x00275646 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
    12  UIKit                               0x00275518 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
    13  UIKit                               0x002755a0 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
    14  UIKit                               0x0027463a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
    15  UIKit                               0x0027459c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
    16  UIKit                               0x002752f3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
    17  UIKit                               0x002788e6 -[UIWindow setDelegate:] + 449
    18  UIKit                               0x00352b77 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
    19  UIKit                               0x0026e474 -[UIWindow addRootViewControllerViewIfPossible] + 591
    20  UIKit                               0x0026e5ef -[UIWindow _setHidden:forced:] + 312
    21  UIKit                               0x0026e86b -[UIWindow _orderFrontWithoutMakingKey] + 49
    22  UIKit                               0x002793c8 -[UIWindow makeKeyAndVisible] + 65
    23  UIKit                               0x00229bc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
    24  UIKit                               0x0022e667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    25  UIKit                               0x00242f92 -[UIApplication handleEvent:withNewEvent:] + 3517
    26  UIKit                               0x00243555 -[UIApplication sendEvent:] + 85
    27  UIKit                               0x00230250 _UIApplicationHandleEvent + 683
    28  GraphicsServices                    0x037e5f02 _PurpleEventCallback + 776
    29  GraphicsServices                    0x037e5a0d PurpleEventCallback + 46
    30  CoreFoundation                      0x0176bca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    31  CoreFoundation                      0x0176b9db __CFRunLoopDoSource1 + 523
    32  CoreFoundation                      0x0179668c __CFRunLoopRun + 2156
    33  CoreFoundation                      0x017959d3 CFRunLoopRunSpecific + 467
    34  CoreFoundation                      0x017957eb CFRunLoopRunInMode + 123
    35  UIKit                               0x0022dd9c -[UIApplication _run] + 840
    36  UIKit                               0x0022ff9b UIApplicationMain + 1225
    37  TEST AutoLayout                     0x000025ed main + 141
    38  libdyld.dylib                       0x01e37701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I am fairly new to Auto Layout (perhaps quite obviously), so any broad explanation of my likely fundamental misunderstandings would be greatly appreciated! 我对自动版面设计还很陌生(也许很明显),所以对我可能的基本误解的任何广泛解释将不胜感激!

Thanks! 谢谢!

While this doesn't directly answer your question about autoLayout, I find doing what you're looking to do in the following way is much simpler: 虽然这不能直接回答有关autoLayout的问题,但我发现以以下方式完成您想要做的事情要简单得多:

- (void)welcomeScreen {
    UIView *newView = [[UIView alloc] initWithFrame:self.view.frame];
    newView.backgroundColor = [UIColor redColor];
    newView.autoresizingMask = UIViewAutoresizingFlexibleHeight + UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:newView];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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