Xcode无法调试视图框架

3

当我使用Debug菜单 > 查看调试信息 > 显示视图框架时,我无法在Xcode中使用调试视图功能,控制台中出现了一个断言日志。

Assertion failure in -[UIVisualEffectView _addSubview:positioned:relativeTo:], 
/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3698.94.10/UIVisualEffectView.m:1859

使用任何项目和开发SDK 10、11、12,我尝试删除Xcode并重新安装它,使用我记得的Xcode 10和10.1。 enter image description here enter image description here 如果创建一个只有一个视图控制器的新项目,它可以正常工作。但一旦将其嵌入导航控制器中,它就停止工作了。我认为isTranslucent正在使用UIVisualEffect,这在断言中失败了。
是否有解决此问题的方法,或者苹果已经知道了吗?
1个回答

0

这很可能是 iOS 中的一个 bug。当点击 View Debug -> Show View Frames 时,UIView 将添加 _UIDebugColorBoundsView_UIDebugAlignmentRectView 作为其子视图,而 UIVisualEffectViewUIView 的一种类型。因此,在 View Debug 时,UIVisualEffectView 将添加一些 _UIDebugXXXViews 作为子视图,但是 不要直接将子视图添加到视觉效果视图本身上。,这会导致崩溃。我们应该将任何子视图添加到视觉效果视图的 contentView 属性中。为了解决这个问题,需要使用 objc 运行时进行一些 hack,如下所示:

#import <objc/runtime.h>

#if DEBUG
@implementation UIView (Debug)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL systemSel       = @selector(addSubview:);
        SEL swizzSel        = @selector(dy_degbugAddSubview:);
        Method systemMethod = class_getInstanceMethod([self class], systemSel);
        Method swizzMethod  = class_getInstanceMethod([self class], swizzSel);
        BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(swizzMethod),
            method_getTypeEncoding(swizzMethod));
        if (isAdd) {
            class_replaceMethod(self, swizzSel, method_getImplementation(systemMethod),
                method_getTypeEncoding(systemMethod));
        } else {
            method_exchangeImplementations(systemMethod, swizzMethod);
        }
    });
}

- (void)dy_degbugAddSubview:(UIView *)view {
    if ([self isKindOfClass:[UIVisualEffectView class]]) {
        // _UIDebugColorBoundsView
        // _UIDebugAlignmentRectView
        if ([[[view class] description] containsString:@"_UIDebug"]) {
            // View DEBUG will assert failure
            [[(UIVisualEffectView *)self contentView] dy_degbugAddSubview:view];
            return;
        }
        [self dy_degbugAddSubview:view];
    } else {
        [self dy_degbugAddSubview:view];
    }
}
@end

#endif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接