Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift UIViewReportBrokenSuperviewChain由层操纵引起_Ios_Xcode_Ios10 - Fatal编程技术网

Ios Swift UIViewReportBrokenSuperviewChain由层操纵引起

Ios Swift UIViewReportBrokenSuperviewChain由层操纵引起,ios,xcode,ios10,Ios,Xcode,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 v

在将代码迁移到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.'
导致该问题的子代码似乎是:

- (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(iOS-MDTextField的材质控件)时遇到了这个库问题。我发现问题来自于一个视图(没有superview)的图层添加到另一个视图的位置


看起来你自己也是这样——你正在创建的工具栏并没有首先添加到superview中。我使用的修复方法是将视图添加为要添加图层的视图的子视图,因此在您的情况下,将工具栏添加为myModelView的子视图应该可以阻止错误。

为什么要操作图层层次结构?也许最好添加一个工具栏作为myModelView的子视图,并确实解决这个问题?好吧,这是遗留代码,所以我只是想在这里解决这个问题。现在我刚刚删除了insertSublayer的问题。我发现这是一种获得透明背景的技巧。显然,这是一个众所周知的技巧,但这使得新的应用程序崩溃。我会尽快更新这篇文章。Hi@Mikael“在UIViewReportBrokenSuperviewChain上中断以进行调试”。你知道这是什么意思吗?我也遇到了这个错误,而且没有很好的文档记录。在本例中,我试图将UIView元素插入到另一个元素中。但是第一个元素没有添加到UIViewController.view中,因此,它会触发一个错误。您在这里清楚地找到了崩溃的原因。如果我添加:[self.myModelView addSubview:_toolbar];在插入层之前,不要崩溃。
- (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];
}