Xcode Can't Debug View Frame

Viewed 409

I can't use the debug View feather in Xcode when I use Debug Menu > View Debugging > Show View Frames there is an Assertion log in the console.

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

With any project and with Development SDK 10, 11, 12 and I tried to delete the Xcode and reinstall it with Xcode 10 and 10.1 as I remember enter image description here enter image description here

It work fine if create a new project with one view Controller . But once embedded it in navigation controller it stoped working , I think the isTranslucent is Using UIVisualEffect witch failed in the Assertion .

Is there a work around this Issues , or it is known for apple?

1 Answers

It's likely a bug in iOS.When click View Debug -> Show View Frames, UIView add _UIDebugColorBoundsView _UIDebugAlignmentRectView as subView, while UIVisualEffectView is kind of UIView.So When View Debug, UIVisualEffectView will add some _UIDebugXXXViews as subView, BUT Do not add subviews directly to the visual effect view itself.,which will result in crash. We should add any subviews to the contentView property of the visual effect view.to fix this, need some hack with objc runtime as below:

#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
Related