我正在尝试将UIView子视图添加到UITableView,该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
我对Auto Layout相当新(也许很明显),所以对我可能存在的基本误解的任何广泛解释都将不胜感激!
谢谢!
答案 0 :(得分:0)
虽然这并没有直接回答你关于autoLayout的问题,但我觉得按照以下方式做你想做的事情要简单得多:
- (void)welcomeScreen {
UIView *newView = [[UIView alloc] initWithFrame:self.view.frame];
newView.backgroundColor = [UIColor redColor];
newView.autoresizingMask = UIViewAutoresizingFlexibleHeight + UIViewAutoresizingFlexibleWidth;
[self.view addSubview:newView];
}