簡體   English   中英

向UITableView添加約束時自動布局崩潰

[英]Auto Layout Crashes When Adding Constraints to UITableView

我試圖將UIView子視圖添加到將隨設備適當旋轉的UITableView中。 我希望子視圖覆蓋整個表格。 這是我的方法,從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]];
}

當我在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.'

這是完整的堆棧跟蹤:

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

我對自動版面設計還很陌生(也許很明顯),所以對我可能的基本誤解的任何廣泛解釋將不勝感激!

謝謝!

雖然這不能直接回答有關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