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

为了适配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 = [[UIView alloc]initWithFrame:self.layer.bounds];

    mohuView.backgroundColor = [UIColor blackColor];

    mohuView.alpha = 0.8;

    [self addSubview:mohuView];

    [self sendSubviewToBack:mohuView];

    

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

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

    self.beiJingImageView.contentMode = UIViewContentModeScaleAspectFit;

    [self addSubview:self.beiJingImageView];

    [self sendSubviewToBack:self.beiJingImageView];


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值