Swift UIViewReportBrokenSuperviewChain 由图层操作引起

Swift UIViewReportBrokenSuperviewChain cause by Layer manipulation

我 运行 在将我的代码迁移到 Swift 3 后遇到了问题。 我想 iOS10 现在提出了新问题,它实际上与 Swift 本身无关。

错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'View has lost track of its superview, most likely through unsupported use of CALayer API on the view's layer. If this isn't a crash yet, it will be in the near future. 
    Problem view: <UIToolbar: 0x102552d80; frame = (0 0; 375 683); alpha = 0.97; opaque = NO; layer = <CALayer: 0x1700383e0>>
    Expected parent: <MyModelView: 0x10250ecd0; frame = (0 -16; 375 683); hidden = YES; layer = <CALayer: 0x17003d4a0>>
Break on UIViewReportBrokenSuperviewChain to debug.'

触发问题的代码是:

[c presentViewController:tabBarViewController animated:NO completion:^{

导致问题的子代码似乎是:

- (void)addBlurView
{
  CGRect viewBounds = [[UIScreen mainScreen]applicationFrame];
  self.myModelView = [[MyModalView alloc] initWithFrame:CGRectMake(viewBounds.origin.x, -16, viewBounds.size.width, viewBounds.size.height+36)];

  if(![self toolbar]) {
    _toolbar = [[UIToolbar alloc] initWithFrame:[self.myModelView bounds]];
    [_toolbar setBarStyle:UIBarStyleBlack];
    _toolbar.alpha = 0.97;
    [self.myModelView.layer insertSublayer:_toolbar.layer atIndex:0];
  }

  [self.view addSubview:self.myModelView];
}

我在转到 Xcode 8 (Material-Controls-For-iOS - MDTextField) 时遇到了这个库问题。我发现问题出在将一个视图(没有超级视图)的层添加到另一个视图的位置。

看起来您自己也可能是这种情况 - 您正在创建的工具栏尚未首先添加到超级视图中。我使用的修复方法是将视图添加为要添加图层的视图的子视图,因此在您的情况下,将工具栏添加为 myModelView 的子视图应该会停止错误。