【发布时间】:2014-06-15 19:44:35
【问题描述】:
我已经尝试了几个小时。我将 UIView 的子类添加到我的 UITableViewController 的视图中。我正在使用这篇文章底部的代码。
我收到一个崩溃提示:
2014-06-15 12:19:58.724 Block Party[4712:60b] *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.137/UIView.m:8794
2014-06-15 12:19:58.726 Block Party[4712: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:
(0x2e940f0b 0x390d7ce7 0x2e940ddd 0x2f2ede2f 0x311703c5 0x30dec31b 0x30de7b3f 0x311854a1 0x3e387 0x311a5a3b 0x311bb8a5 0x3e221 0x3d267 0x31174a53 0x31174811 0x31300c13 0x3121e48f 0x3121e299 0x3121e231 0x31170305 0x30dec31b 0x30de7b3f 0x30de79d1 0x30de73e5 0x30de71f7 0x30de0f1d 0x2e90c039 0x2e9099c7 0x2e909d13 0x2e874769 0x2e87454b 0x337e16d3 0x311d3891 0x3acb5 0x395d5ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
我试图通过添加 layoutSublayerofLayer 方法来解决这个问题,但它并没有改变任何东西。另外,如果我注释掉 [self.locationNeededView setTranslatesAutoresizingMaskIntoConstraints:NO];那么它不会崩溃,但我的约束被忽略了,因为 autoresizingmaskconstraints 优先。
我完全不知道如何解决这个问题。基本上我希望这个视图出现在我的桌子上,水平和垂直居中。
提前感谢您提供的任何帮助。
-杰夫
- (void) layoutSublayersOfLayer:(CALayer *)layer {
[super layoutSublayersOfLayer:layer];
}
- (void)loadLocationNeededView {
UINib *nib = [UINib nibWithNibName:@"LocationNeededUIView" bundle:nil];
self.locationNeededView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
[self.locationNeededView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem: self.view
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.locationNeededView
attribute:NSLayoutAttributeCenterX
multiplier:0
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem: self.view
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.locationNeededView
attribute:NSLayoutAttributeCenterY
multiplier:0
constant:0]];
[self.view addSubview:self.locationNeededView];
[self.view layoutSubviews];
}
编辑 - 更新
我放弃了使用约束,我只是使用框架。我让它与以下代码一起工作得很好。我还发现如果我将它添加到导航控制器的视图中,那么它不会随着 tableview 滚动。这正是我想要的。
- (void)loadLocationNeededView {
//allocate subview
UINib *nib = [UINib nibWithNibName:@"LocationNeededUIView" bundle:nil];
self.locationNeededView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
//set frame for it offscreen so it can be animated in
self.locationNeededView.frame = CGRectMake((self.view.frame.size.width / 2) - (self.locationNeededView.frame.size.width / 2), [[UIScreen mainScreen] bounds].size.height, self.locationNeededView.frame.size.width, self.locationNeededView.frame.size.height);
//addsubview to navigation controller so that it does not scroll with the tableview
[self.navigationController.view addSubview:self.locationNeededView];
[self animateLocationNeededViewIn];
}
- (void) animateLocationNeededViewIn {
[UIView animateWithDuration:0.35
delay:1
usingSpringWithDamping:0.7
initialSpringVelocity:1.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^ {
self.locationNeededView.frame = CGRectMake((self.view.frame.size.width / 2) - (self.locationNeededView.frame.size.width / 2), (self.view.frame.size.height / 2) - (self.locationNeededView.frame.size.height / 2), self.locationNeededView.frame.size.width, self.locationNeededView.frame.size.height);
[self.view layoutIfNeeded];} completion:^ (BOOL fin) {
if (fin) {
NSLog(@"After Animating View Frame: Origin X = %f. Origin Y = %f. Width = %f. Height = %f", self.locationNeededView.frame.origin.x, self.locationNeededView.frame.origin.y, self.locationNeededView.frame.size.width, self.locationNeededView.frame.size.height);
}
}];
}
【问题讨论】:
-
你是在
layoutSubviews方法中调用[super layoutSubviews];吗? -
您希望该视图随表格一起滚动吗?如果没有,您应该使用 UIViewController,因为您添加到 UITableViewController 的任何视图都会成为表格的子视图。
-
在我的 UIView 子类的实现中,我声明了 -(void) layoutSubviews { [super layoutSubviews];另外,当我尝试展示 modalviewcontroller 时,它似乎不支持 uiviewcontroller 的清晰背景。它只是变黑并覆盖用户最初所在的屏幕。但我想我可能会再试一次,因为我遇到了很多麻烦。
-
您的约束乘数错误。它们应该是 1。基本上,您将视图限制为 0。
标签: objective-c uitableview uiview constraints autolayout