iOS13 适配 夜间模式(深色模式 DarkMode)与其他
- 夜间模式
- 其他问题:presentViewController
###一 :夜间/深色模式 DarkMode
夜间模式是iOS13的重要更新之一,随之而来的是我们能从系统设置中“显示与亮度”中选择“浅色”、“深色”两种模式,并且可以设置自动切换。(“控制中心”亮度调节中也可直接调节)
已知问题:在系统设置为深色模式时候,无法更改StateBar颜色
-
如果不想适配深色模式 (1).直接在项目的plist文件中设置
<key>UIUserInterfaceStyle</key> <string>UIUserInterfaceStyleLight</string>(2).在每个UIViewController或者BaseViewController(如果自己有的话),中设置
if (@available(iOS 13.0, *)){ self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight; } -
适配深色模式 首先我们要看一下显示模式的枚举值
typedef NS_ENUM(NSInteger, UIUserInterfaceStyle) {
UIUserInterfaceStyleUnspecified,
UIUserInterfaceStyleLight,
UIUserInterfaceStyleDark,
} API_AVAILABLE(tvos(10.0)) API_AVAILABLE(ios(12.0)) API_UNAVAILABLE(watchos);
当前API还没有提供浅色/深色模式切换时的通知,但是为UIColor添加了新方法:
+ (UIColor *)colorWithDynamicProvider:(UIColor * (^)(UITraitCollection *))dynamicProvider;
该方法通过一个block返回颜色,根据其中UITraitCollection参数,我们可以获取到当前系统的UIUserInterfaceStyle. 这个方法会在每次系统模式改变后回调,所以我想,我可以在一个颜色中去为当前界面做监听.
Xcode 11为xcassets带来更新以自动读取加载浅色/深色模式的资源,只要修改资源Appearances属性,来设置是否要支持浅色/深色模式,以及资源内容即可,[UIImage imageNamed:@""]会自动加载浅色/深色资源.

最后上一段 UIViewController 截图

最后最后上一段 UIViewController 代码
#import "DarkModeViewController.h"
@interface DarkModeViewController ()
{
UIImageView *_iv2;
UIUserInterfaceStyle _userInterfaceStyle;
}
@end
@implementation DarkModeViewController
- (void)viewDidLoad {
[super viewDidLoad];
__weak typeof(self)weakSelf = self;
UIColor *backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * traitCollection) {
self->_userInterfaceStyle = traitCollection.userInterfaceStyle;
[weakSelf performSelector:@selector(traitCollectionChanged:) withObject:traitCollection];
if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
return [UIColor blueColor];
}
return [UIColor yellowColor];
}];
self.view.backgroundColor = backgroundColor;
UIImageView *iv = ({
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setFrame:CGRectMake(20, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"icon_star"];
imageView;
});
[self.view addSubview:iv];
_iv2 = ({
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setFrame:CGRectMake(20, 240, 100, 100)];
imageView;
});
[self.view addSubview:_iv2];
[self iv2updateImage];
}
- (void)traitCollectionChanged:(UITraitCollection *)traitCollection{
NSLog(@"traitCollectionChanged:%ld",traitCollection.userInterfaceStyle);
[self iv2updateImage];
}
- (void)iv2updateImage {
NSLog(@"iv2updateImage:%ld",_userInterfaceStyle);
if (_userInterfaceStyle == UIUserInterfaceStyleDark) {
_iv2.image = [UIImage systemImageNamed:@"star.circle.fill"];
}else{
_iv2.image = [UIImage systemImageNamed:@"star.circle"];
}
}
@end
###二 :其他问题
- presentViewController modalPresentationStyle参数有 iOS12 之前的UIModalPresentationFullScreen改为了UIModalPresentationPageSheet, 在需要presentViewController FullScreen样式,需要提前设置
相关文章
- 苹果上怎么调时间 苹果的手机萤幕看小说的时候怎么调成夜间模式,就是萤幕黑,字白
- 越狱后的插件 iOS9.02越狱后有哪些插件可以用的
- 苹果打电话拿开不亮屏 iphone手机怎么设置来电闪光灯
- 苹果8待机一晚上耗电 iphone 6 plus待机耗电快怎么办?
- x进恢复模式 手机开启了夜间模式怎么恢复
- 小米手机 夜间模式 小米手机设置中为什么没有夜间模式一项本人使用中,不知何时变成夜间模式了
- 苹果iOS12越狱工具 苹果手机越狱后系统会不会没有纯苹果系统这么好
- iOS13屏幕录制 iphone 升级ios6.1 失败 无限恢复模式 屏幕一直显示 连接itunes 画面拜托了各位 谢谢
- 苹果7p一晚上掉电多少正常 iphone6plus代机一晚上大概掉多少电
- iPhone 11好用吗 HighLight(iPhone套用)详细资料大全
爱学记

微信收款码
支付宝收款码