Skip to content

Instantly share code, notes, and snippets.

@timonus
Last active July 3, 2016 22:33
Show Gist options
  • Save timonus/206487f8466dde841ccdd26fca4fb752 to your computer and use it in GitHub Desktop.
Save timonus/206487f8466dde841ccdd26fca4fb752 to your computer and use it in GitHub Desktop.
tvOS 10 Dark Mode Helper
//
// UITraitCollection+DarknessCompatibility.h
// Tangent
//
// Created by Tim Johnsen on 6/25/16.
// Copyright © 2016 tijo. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UITraitCollection (DarknessCompatibility)
- (BOOL)tj_isDark;
- (BOOL)tj_hasSameDarkSettingAs:(UITraitCollection *const)traitCollection;
@end
//
// UITraitCollection+DarknessCompatibility.m
// Tangent
//
// Created by Tim Johnsen on 6/25/16.
// Copyright © 2016 tijo. All rights reserved.
//
#import "UITraitCollection+DarknessCompatibility.h"
@implementation UITraitCollection (DarknessCompatibility)
- (BOOL)tj_isDark
{
BOOL isDark = NO;
// TODO: Use actual selector and enum value once building for iOS 10.
static BOOL isTvOS1OrHigher = NO;
static SEL userInterfaceStyleSelector = nil;
static NSInvocation *userInterfaceStyleInvocation = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){10, 0, 0}]) {
isTvOS1OrHigher = YES;
userInterfaceStyleSelector = NSSelectorFromString(@"userInterfaceStyle");
userInterfaceStyleInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:userInterfaceStyleSelector]];
userInterfaceStyleInvocation.selector = userInterfaceStyleSelector;
}
});
if (isTvOS1OrHigher) {
[userInterfaceStyleInvocation invokeWithTarget:self];
NSInteger returnValue = 0;
[userInterfaceStyleInvocation getReturnValue:&returnValue];
static const NSInteger tj_UIUserInterfaceStyleDark = 2;
isDark = (returnValue == tj_UIUserInterfaceStyleDark);
}
return isDark;
}
- (BOOL)tj_hasSameDarkSettingAs:(UITraitCollection *const)traitCollection
{
return [self tj_isDark] == [traitCollection tj_isDark];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment