Adapter iOS DarkMode
-
对于iOS13+ 跟随系统暗黑开关
- 也支持自定义APP内开关,设置即可【参考下文说明】
-
对于iOS13以下,则默认Light模式
- 也可以指定自定义模式,设置即可【参考下文说明】
-
获取状态切换【模式切换检测】
- CGColor并不会动态改变的解决方案
- 常规项目的暗黑场景处理
To run the example project, clone the repo, and run pod install
from the
Example directory first.
LFLDarkModeKit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'LFLDarkModeKit', '~> 3.2.0'
- Layout Class
.
├── Core
│ ├── LFLDarkModeManger.h
│ ├── LFLDarkModeManger.m
│ ├── UIColor+LFLDarkMode.h
│ ├── UIColor+LFLDarkMode.m
│ ├── UIView+LFLDarkMode.h
│ └── UIView+LFLDarkMode.m
├── LFLDarkModeKit.h
└── Tool
├── NSString+DarkModeKitBlank.h
└── NSString+DarkModeKitBlank.m
└── UIWindow+DarkModeKitKeyWondow.h
└── UIWindow+DarkModeKitKeyWondow.m
各项目自定义色值字符串映射对应颜色
-
Example:@“PColor0” 在dark和light分别对应不同的真实色值一一映射,后续可支持新增模式
-
Example:darkModeAdapterColor.bundle (Color Set )
- dark.plist
- light.plist
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSURL *darkModeBundleURL = [[NSBundle mainBundle] URLForResource:@"darkModeAdapterColor" withExtension:@"bundle"];
[[LFLDarkModeManger sharedInstance] configDarkModeColorBundleURL:darkModeBundleURL];
return YES;
}
// 配置开启用户自定义开关模式
[LFLDarkModeManger.sharedInstance configUserDarkMode:YES];
// 获取当前用户自定义是否暗黑模式
[LFLDarkModeManger.sharedInstance isUserDarkMode];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_darkModeNoti:) name:LFLDarkModeChangeNotification object:nil];
- (void)_darkModeNoti:(NSNotification *)noti {
NSDictionary *darkModeDic = noti.object;
NSLog(@"\n通知:暗黑模式切换检测%@",darkModeDic);
}
// view
self.exampleLabel.textColor = [UIColor ColorAdpterWithHex:@"DEMO"];
// Imageview
self.adapterImageView.image = [UIImage imageAdapterNamed:@"exampleImage"];
// Layer
self.customView.layerBorderColorHex = @"DEMO";
全局关闭暗黑模式:在Info.plist文件中,添加UIUserInterfaceStyle key 名字为 User Interface Style 值为String,将UIUserInterfaceStyle key 的值设置为Light
- 如果项目中有需要适配暗黑模式的图片,可以在Assets.xcassets中设置,具体需要什么样式自己根据项目情况设置
- any dark
- iOS13 API (当系统切换模式的时候,自动触发这两个方法来动态修改控件颜色)
+ (UIColor *)colorWithDynamicProvider:(UIColor * (^)(UITraitCollection *traitCollection))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchOS);
- (UIColor *)initWithDynamicProvider:(UIColor * (^)(UITraitCollection *traitCollection))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);
设置的CGColor并不会动态改变,可以通过以下方案处理
如果适配CGColor,一般需要各自自定义view实现此函数再处理,较为麻烦。
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
// Config Color Adapter
}
}
- performAsCurrent
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
[self.traitCollection performAsCurrentTraitCollection:^{
layer.backgroundColor = xxColor.CGColor;
}];
}
if (@available(iOS 13.0, *)) {
[self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark];
} else {
}
- color set
- any dark
- [UIColor colorNamed:@"customBlueColor"]
NSDictionary * attributeDic = @{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor xxColor]};
typedef NS_ENUM(NSInteger, UIActivityIndicatorViewStyle) {
UIActivityIndicatorViewStyleMedium,
UIActivityIndicatorViewStyleLarge,
UIActivityIndicatorViewStyleWhiteLarge API_DEPRECATED_WITH_REPLACEMENT("UIActivityIndicatorViewStyleLarge",
UIActivityIndicatorViewStyleWhite API_DEPRECATED_WITH_REPLACEMENT("UIActivityIndicatorViewStyleMedium",
UIActivityIndicatorViewStyleGray API_DEPRECATED_WITH_REPLACEMENT("UIActivityIndicatorViewStyleMedium",
};
-
UIView
- traitCollectionDidChange(_:)
- layoutSubviews()
- draw(_:)
- updateConstraints()
- tintColorDidChange()
-
UIViewController
- traitCollectionDidChange(_:)
- updateViewConstraints()
- viewWillLayoutSubviews()
- viewDidLayoutSubviews()
-
UIPresentationController
- traitCollectionDidChange(_:)
- containerViewWillLayoutSubviews()
- containerViewDidLayoutSubviews()
LFLDarkModeKit is available under the MIT license. See the LICENSE file for more info.