Skip to content

Instantly share code, notes, and snippets.

@iccir
Last active May 6, 2021 00:47
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iccir/c0b0f69d71e272e07ead2f631a4df942 to your computer and use it in GitHub Desktop.
Save iccir/c0b0f69d71e272e07ead2f631a4df942 to your computer and use it in GitHub Desktop.
iOS Dark Mode Toggle
/*
During Dark Mode migration for macOS, I found it helpful to have a global hotkey
which toggled between Light/Dark Mode.
This hack attempts to do something similar for iOS.
1) Add your main window in -applicationDidFinishLaunching:
2) Triple tap the window (I tend to do this near the title bar) to flip between light and dark.
*/
#if DEBUG
@interface UIApplication ()
+ (void) _setDebugUserInterfaceStyleOverride:(NSInteger)override;
@end
@implementation UIApplication (IccirDebugUserInterfaceStyleOverride)
- (void) iccirToggleDebugUserInterfaceStyleOverride
{
static NSInteger sOverride = 0;
sOverride = sOverride == 1 ? 2 : 1;
[UIApplication _setDebugUserInterfaceStyleOverride:sOverride];
}
@end
void AddInterfaceStyleToggle(UIWindow *window)
{
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] init];
[recognizer setNumberOfTapsRequired:3];
[recognizer addTarget:[UIApplication sharedApplication] action:@selector(iccirToggleDebugUserInterfaceStyleOverride)];
[window addGestureRecognizer:recognizer];
}
#else
void AddInterfaceStyleToggle(UIWindow *window) { }
#endif
@iccir
Copy link
Author

iccir commented Jun 10, 2019

Xcode has a nice Environment Overrides pop-up, but I find this to be way more convenient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment