IOS10上崩溃错误“View has lost track of its superview, most likely through unsupported use of CALayer”解决方案

来源:互联网 发布:阿里云域名过户步骤 编辑:程序博客网 时间:2024/06/15 18:51

为了适配iOS 10 升级了Xcode 8,项目中点击cell中的一个按钮就崩溃了。

崩溃日志: 

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: 0x1018555d0; frame = (0 0; 0 0); layer = <CALayer: 0x17023f520>>
    Expected parent: <AMBlurView: 0x1018697c0; frame = (0 0; 320 160); clipsToBounds = YES; alpha = 0.8; autoresize = W+H; layer = <CALayer: 0x17023f1e0>>

Break on UIViewReportBrokenSuperviewChain to debug.'


在谷歌上找到了问题的原因,如下所示:


这是insertsublayer问题。这是一种把戏获得透明度的背景。显然这是一个已知的把戏,但这使得应用程序崩溃在新的一个。


在项目中找到了问题代码,是因为我写的视频播放器要在播放时在底层添加一个模糊背景图片,我是像下面这样写的

   //当播放的时候在底层设置图片(填充播放剩余的部分)
     self.beiJingImageView = [[UIImageView alloc]initWithFrame:self.layer.bounds];
    [self.layer insertSublayer:_beiJingImageView.layer below:_playerLayer];
    
    //给背景图片添加模糊效果
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    self.effectView.frame = self.beiJingImageView.bounds;
    //设置模糊透明度
    self.effectView.alpha = .8f;
    [self.beiJingImageView addSubview:self.effectView];


引起崩溃的代码就是下面代码

 self.beiJingImageView = [[UIImageView alloc]initWithFrame:self.layer.bounds];

 [self.layer insertSublayer:_beiJingImageView.layer below:_playerLayer];


所以换了新的实现方式

    //先设置模糊层

    UIView *mohuView = [[UIViewalloc]initWithFrame:self.layer.bounds];

    mohuView.backgroundColor = [UIColorblackColor];

    mohuView.alpha =0.8;

    [selfaddSubview:mohuView];

    [selfsendSubviewToBack:mohuView];

    

//    //当播放的时候在底层设置图片(填充播放剩余的部分)

    self.beiJingImageView = [[UIImageViewalloc]initWithFrame:self.layer.bounds];

    self.beiJingImageView.contentMode =UIViewContentModeScaleAspectFit;

    [selfaddSubview:self.beiJingImageView];

    [selfsendSubviewToBack:self.beiJingImageView];


这就是我的这个崩溃的解决方式,如果正好你碰到了,正好可以尝试这个方法解决一下


0 0
原创粉丝点击