将我的代码迁移到 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) 时遇到了这个库问题。我发现问题出在将一个 View (没有 super View )的层添加到另一个 View 的位置。
看起来您自己也可能是这种情况 - 您正在创建的工具栏尚未首先添加到 super View 中。我使用的修复方法是将 View 添加为要添加图层的 View 的 subview ,因此在您的情况下,将工具栏添加为 myModelView 的 subview 应该会停止错误。
关于ios - Swift UIViewReportBrokenSuperviewChain 由 Layer 操作引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39565424/