UIKit interoperablility in iOS 13
This commit is contained in:
Родитель
facaafc73d
Коммит
e26dbe4291
|
@ -55,6 +55,8 @@
|
|||
8CE066CF239E5582002CE16D /* UIColor+DarkModeKit.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CC8D86C2398E4EC0043276A /* UIColor+DarkModeKit.m */; };
|
||||
8CE066D0239E5586002CE16D /* UIImage+DarkModeKit.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CC8D8722398E7A30043276A /* UIImage+DarkModeKit.m */; };
|
||||
EA7316F1248F5055009AE037 /* UILabelVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA7316EF248F5050009AE037 /* UILabelVC.swift */; };
|
||||
FCB3499E24975D8600192E96 /* DMDarkModeManager.h in Headers */ = {isa = PBXBuildFile; fileRef = FCB3499C24975D8600192E96 /* DMDarkModeManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
FCB3499F24975D8600192E96 /* DMDarkModeManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FCB3499D24975D8600192E96 /* DMDarkModeManager.m */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
|
@ -149,6 +151,8 @@
|
|||
8CDA628A2366DAA9004895B5 /* DarkModeManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DarkModeManager.swift; sourceTree = "<group>"; };
|
||||
8CDA628B2366DAA9004895B5 /* DMDynamicColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DMDynamicColor.h; sourceTree = "<group>"; };
|
||||
EA7316EF248F5050009AE037 /* UILabelVC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UILabelVC.swift; sourceTree = "<group>"; };
|
||||
FCB3499C24975D8600192E96 /* DMDarkModeManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DMDarkModeManager.h; sourceTree = "<group>"; };
|
||||
FCB3499D24975D8600192E96 /* DMDarkModeManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DMDarkModeManager.m; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
|
@ -208,6 +212,8 @@
|
|||
isa = PBXGroup;
|
||||
children = (
|
||||
8CDA62702366DAA9004895B5 /* FluentDarkModeKit.h */,
|
||||
FCB3499C24975D8600192E96 /* DMDarkModeManager.h */,
|
||||
FCB3499D24975D8600192E96 /* DMDarkModeManager.m */,
|
||||
8CDA628B2366DAA9004895B5 /* DMDynamicColor.h */,
|
||||
8CDA62732366DAA9004895B5 /* DMDynamicColor.m */,
|
||||
8CDA62762366DAA9004895B5 /* DMDynamicImage.h */,
|
||||
|
@ -337,6 +343,7 @@
|
|||
8CC8D8702398E7520043276A /* DMNamespace.h in Headers */,
|
||||
8CDA628D2366DAA9004895B5 /* DMTraitCollection.h in Headers */,
|
||||
8CDA628C2366DAA9004895B5 /* FluentDarkModeKit.h in Headers */,
|
||||
FCB3499E24975D8600192E96 /* DMDarkModeManager.h in Headers */,
|
||||
8CB63E41238551F3008ABCE2 /* UIView+DarkModeKit.h in Headers */,
|
||||
8CC8D8732398E7A30043276A /* UIImage+DarkModeKit.h in Headers */,
|
||||
8CDA62912366DAA9004895B5 /* DMDynamicImage.h in Headers */,
|
||||
|
@ -541,6 +548,7 @@
|
|||
8CDA628F2366DAA9004895B5 /* DMDynamicColor.m in Sources */,
|
||||
8CDA62A42366DAA9004895B5 /* DarkModeManager.swift in Sources */,
|
||||
8CDA62982366DAA9004895B5 /* UITextView+DarkModeKit.swift in Sources */,
|
||||
FCB3499F24975D8600192E96 /* DMDarkModeManager.m in Sources */,
|
||||
8CDA62942366DAA9004895B5 /* UIView+DarkModeKit.swift in Sources */,
|
||||
8CDA62A32366DAA9004895B5 /* DMDynamicImage.m in Sources */,
|
||||
8CDA628E2366DAA9004895B5 /* DMTraitCollection.m in Sources */,
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DMDarkModeManager : NSObject
|
||||
|
||||
@property(class, nonatomic, assign) BOOL interoperableWithUIKit;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
|
@ -0,0 +1,20 @@
|
|||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
//
|
||||
|
||||
#import "DMDarkModeManager.h"
|
||||
|
||||
static BOOL _interoperableWithUIKit;
|
||||
|
||||
@implementation DMDarkModeManager
|
||||
|
||||
+ (BOOL)interoperableWithUIKit {
|
||||
return _interoperableWithUIKit;
|
||||
}
|
||||
|
||||
+ (void)setInteroperableWithUIKit:(BOOL)interoperableWithUIKit {
|
||||
_interoperableWithUIKit = interoperableWithUIKit;
|
||||
}
|
||||
|
||||
@end
|
|
@ -4,12 +4,20 @@
|
|||
//
|
||||
|
||||
#import "DMTraitCollection.h"
|
||||
#import "DMDarkModeManager.h"
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@implementation DMTraitCollection
|
||||
|
||||
static DMTraitCollection *_currentTraitCollection = nil;
|
||||
|
||||
+ (DMTraitCollection *)currentTraitCollection {
|
||||
if (@available(iOS 13, *)) {
|
||||
if (DMDarkModeManager.interoperableWithUIKit) {
|
||||
return (DMTraitCollection *)UITraitCollection.currentTraitCollection;
|
||||
}
|
||||
}
|
||||
|
||||
return _currentTraitCollection;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
// Licensed under the MIT License.
|
||||
//
|
||||
|
||||
#import <FluentDarkModeKit/DMDarkModeManager.h>
|
||||
#import <FluentDarkModeKit/DMDynamicColor.h>
|
||||
#import <FluentDarkModeKit/DMDynamicImage.h>
|
||||
#import <FluentDarkModeKit/DMTraitCollection.h>
|
||||
|
|
|
@ -5,10 +5,24 @@
|
|||
|
||||
#import "UIColor+DarkModeKit.h"
|
||||
#import "DMDynamicColor.h"
|
||||
#import "DMDarkModeManager.h"
|
||||
|
||||
@implementation UIColor (DarkModeKit)
|
||||
|
||||
+ (UIColor *)dm_colorWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor {
|
||||
if (@available(iOS 13, *)) {
|
||||
if (DMDarkModeManager.interoperableWithUIKit) {
|
||||
return [UIColor colorWithDynamicProvider:^UIColor * (UITraitCollection *traitCollection) {
|
||||
if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
|
||||
return darkColor;
|
||||
}
|
||||
else {
|
||||
return lightColor;
|
||||
}
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
return (UIColor *)[[DMDynamicColor alloc] initWithLightColor:lightColor darkColor:darkColor];
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#import "UIImage+DarkModeKit.h"
|
||||
#import "DMDynamicImage.h"
|
||||
#import "DMDarkModeManager.h"
|
||||
|
||||
@import ObjectiveC;
|
||||
|
||||
|
@ -36,6 +37,14 @@
|
|||
}
|
||||
|
||||
+ (UIImage *)dm_imageWithLightImage:(UIImage *)lightImage darkImage:(UIImage *)darkImage {
|
||||
if (@available(iOS 13, *)) {
|
||||
if (DMDarkModeManager.interoperableWithUIKit) {
|
||||
UIImage *image = [lightImage copy];
|
||||
[image.imageAsset registerImage:darkImage withTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark]];
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
return (UIImage *)[[DMDynamicImageProxy alloc] initWithLightImage:lightImage darkImage:darkImage];
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#import "UIView+DarkModeKit.h"
|
||||
#import "DMDynamicColor.h"
|
||||
#import "DMDarkModeManager.h"
|
||||
|
||||
@import ObjectiveC;
|
||||
|
||||
|
@ -13,6 +14,12 @@
|
|||
+ (void)dm_swizzleSetBackgroundColor {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
if (@available(iOS 13, *)) {
|
||||
if (DMDarkModeManager.interoperableWithUIKit) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
SEL selector = @selector(setBackgroundColor:);
|
||||
Method method = class_getInstanceMethod(self, selector);
|
||||
if (!method)
|
||||
|
|
|
@ -8,8 +8,12 @@ import UIKit
|
|||
@_exported import DarkModeCore
|
||||
#endif
|
||||
|
||||
public final class DarkModeManager: NSObject {
|
||||
public final class DarkModeManager: DMDarkModeManager {
|
||||
public static func setup() {
|
||||
if #available(iOS 13, *), interoperableWithUIKit {
|
||||
return
|
||||
}
|
||||
|
||||
// Colors
|
||||
UIView.swizzleWillMoveToWindowOnce
|
||||
UIView.dm_swizzleSetBackgroundColor()
|
||||
|
@ -32,6 +36,12 @@ public final class DarkModeManager: NSObject {
|
|||
/// - animated: Use animation or not.
|
||||
@objc public static func updateAppearance(for application: UIApplication, animated: Bool) {
|
||||
application.updateAppearance(with: application.windows, animated: animated)
|
||||
|
||||
if #available(iOS 13, *) {
|
||||
for window in application.windows {
|
||||
window.overrideUserInterfaceStyle = DMTraitCollection.current.userInterfaceStyle.uiUserInterfaceStyle
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Internal
|
||||
|
@ -43,6 +53,22 @@ public final class DarkModeManager: NSObject {
|
|||
|
||||
// MARK: -
|
||||
|
||||
extension DMUserInterfaceStyle {
|
||||
|
||||
@available(iOS 13, *)
|
||||
var uiUserInterfaceStyle: UIUserInterfaceStyle {
|
||||
switch self {
|
||||
case .unspecified: return .unspecified
|
||||
case .dark: return .dark
|
||||
case .light: return .light
|
||||
@unknown default: return .unspecified
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: -
|
||||
|
||||
extension DMTraitEnvironment {
|
||||
/// Trigger `themeDidChange()`.
|
||||
///
|
||||
|
|
|
@ -6,6 +6,11 @@
|
|||
extension UIView: DMTraitEnvironment {
|
||||
open func dmTraitCollectionDidChange(_ previousTraitCollection: DMTraitCollection?) {
|
||||
subviews.forEach { $0.dmTraitCollectionDidChange(previousTraitCollection) }
|
||||
|
||||
if #available(iOS 13, *), DarkModeManager.interoperableWithUIKit {
|
||||
return
|
||||
}
|
||||
|
||||
setNeedsLayout()
|
||||
setNeedsDisplay()
|
||||
dm_updateDynamicColors()
|
||||
|
|
Загрузка…
Ссылка в новой задаче