Swift UIViewReportBrokenSuperviewChain是由图层操作引起的

在将我的代码迁移到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)时,我遇到了这个问题。 我发现问题来自一个视图(没有超视图)的图层被添加到另一个视图的位置。

它看起来也可能是你自己的情况 – 你创build的工具栏还没有被添加到超级视图中。 我使用的修补程序是添加视图作为图层被添加到视图的子视图,所以在您的情况下添加工具栏作为myModelView的子视图应该停止错误。