Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift UIViewReportBrokenSuperviewChain cause by Layer manipulation

Tags:

xcode

ios

ios10

I run into a problem after migrating my code to Swift 3. I guess iOS10 raises new issues now and it's actually not related to Swift itself.

The error:

*** 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.'

The code triggering the issue is :

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

The sub code responsible for the problem seems to be:

- (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];
}
like image 893
Mikael Avatar asked Sep 19 '16 04:09

Mikael


1 Answers

I had this issue with a library when moving over to Xcode 8 (Material-Controls-For-iOS - MDTextField). I found that the problem was coming from where the layer of one view (which had no superview) was being added to another.

It looks like this may be the case for yourself also - your toolbar being created has not been added to a superview first. The fix I used was to add the view as a subview of the view that the layer was being added to, so in your case adding the toolbar as a subview of myModelView should stop the error.

like image 171
DavidBennettUK Avatar answered Sep 28 '22 11:09

DavidBennettUK