Adapter iOS DarkMode
对于iOS13+ 跟随系统暗黑开关
对于iOS13以下,则默认Light模式
获取状态切换【模式切换检测】
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'
.
├── 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 )
- (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
+ (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
}
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
[self.traitCollection performAsCurrentTraitCollection:^{
layer.backgroundColor = xxColor.CGColor;
}];
}
if (@available(iOS 13.0, *)) {
[self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark];
} else {
}
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
UIViewController
UIPresentationController
Dragonli_52171@163.com
LFLDarkModeKit is available under the MIT license. See the LICENSE file for more info.