Swift UIViewReportBrokenSuperviewChain由Layer操作引起

Mik*_*ael 11 xcode ios ios10

我将代码迁移到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.'
Run Code Online (Sandbox Code Playgroud)

触发问题的代码是:

[c presentViewController:tabBarViewController animated:NO completion:^{
Run Code Online (Sandbox Code Playgroud)

负责该问题的子代码似乎是:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*tUK 12

移动到Xcode 8(Material-Controls-For-iOS - MDTextField)时,我遇到了这个问题.我发现问题来自于一个视图层(没有超视图)被添加到另一个视图的层.

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