iOS7 Autolayout问题 - 将UIView子视图添加到UITableViewController

时间:2014-06-15 19:44:36

标签: objective-c uitableview uiview constraints autolayout

我已经尝试了几个小时。我在UITableViewController的视图中添加了UIView的子类。我正在使用这篇文章底部的代码。

我得到一个崩溃说:

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优先。

我完全不知道如何解决这个问题。基本上我希望这个视图出现在我的桌面上,水平和垂直居中。

提前感谢您提供的任何帮助。

-Jeff


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

1 个答案:

答案 0 :(得分:0)

问题:约束乘数应该是1,而不是0.此外,您不需要调用layoutSubviews,只需调用layoutIfNeeded。除非您在视图中定义了intrinsiccontentSize,否则您的视图需要宽度和高度限制。

相关问题