为了适配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];
这就是我的这个崩溃的解决方式,如果正好你碰到了,正好可以尝试这个方法解决一下