Created
May 13, 2022 21:27
-
-
Save insidegui/c2712c3acaa5af92ced9d2931d0bdcad to your computer and use it in GitHub Desktop.
Revisions
-
insidegui created this gist
May 13, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ #if DEBUG /* This fixes SwiftUI previews not rendering translucent materials correctly by swizzling a couple of properties on NSWindow. Just drop into your project and add to the target being previewed (or something it links against). Notice the #if DEBUG, so this code won't end up in release builds. It also checks for the XCODE_RUNNING_FOR_PREVIEWS environment variable so that it won't affect regular debug builds of the app. */ @import ObjectiveC.runtime; @interface FixSwiftUIMaterialInPreviews : NSObject @end @implementation FixSwiftUIMaterialInPreviews + (void)load { NSDictionary *env = [NSProcessInfo processInfo].environment; BOOL isSwiftUIPreview = [env[@"XCODE_RUNNING_FOR_PREVIEWS"] boolValue]; if (!isSwiftUIPreview) return; NSLog(@"😇 Installing fix for translucency in SwiftUI previews"); Method hasKeyAppearance = class_getInstanceMethod([NSWindow class], NSSelectorFromString(@"hasKeyAppearance")); Method hasMainAppearance = class_getInstanceMethod([NSWindow class], NSSelectorFromString(@"hasMainAppearance")); Method override = class_getClassMethod([self class], @selector(__hasKeyAppearanceOverride)); if (hasKeyAppearance) method_exchangeImplementations(hasKeyAppearance, override); if (hasMainAppearance) method_exchangeImplementations(hasMainAppearance, override); } + (BOOL)__hasKeyAppearanceOverride { return YES; } @end #endif