暗黑模式(黑暗模式,深色模式)适配

  • 全局关闭

打开Info.plist 项目配置文件,添加UIUserInterfaceStyle字段,key类型为String, 其value值设置为 Light模式。

  • 部分关闭

iOS13之后UIViewController与UIView 都新增一个属性 overrideUserInterfaceStyle。该属性设置后,会影响视图控制器和子视图控制器以及父视图和子视图,但不会影响application和present出的视图控制器。

/* Set `overrideUserInterfaceStyle` to cause this view controller and its children to have a* specific `UIUserInterfaceStyle`. This does not affect the application or any parent view controllers.*/
@property (nonatomic) UIUserInterfaceStyle overrideUserInterfaceStyle API_AVAILABLE(tvos(13.0), ios(13.0)) API_UNAVAILABLE(watchos); // Defaults to UIUserInterfaceStyleUnspecified
  • 适配暗黑模式

颜色适配

  • UIColor 动态颜色systemBackgroundColorlabelColorsystemGray2Color等,自己可以试试切换模式后会有什么效果。
    if (@available(iOS 13.0, *)) {self.view.backgroundColor = [UIColor systemBackgroundColor];} else {// Fallback on earlier versionsself.view.backgroundColor = [UIColor whiteColor];}
  • UIColor的colorWithDynamicProviderinitWithDynamicProviderresolvedColorWithTraitCollection三个方法。
    if (@available(iOS 13.0, *)) {col_lineView.backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {return [UIColor whiteColor];}else {return [UIColor blackColor];}}];} else {// Fallback on earlier versionscol_lineView.backgroundColor = [UIColor blackColor];}

图片适配

(1)在Assets.xcassets图片资源库里新建一个Image Set

(2)打开侧边栏,找到最后一项,设置Image Set属性Appearance为Any,Dark。

(3)修改图片名称(所有图片名都一样),拖动图片到不同模式下的对应图片资源里,1倍图可以不用

至此大功告成,可以运行看看效果。

监听模式切换

苹果提供了2个方法,一个是监听模式切换的方法,一个是判断当前的TraitCollection和不同的TraitCollection比较,查看是否能显示不同模式

/*! To be overridden as needed to provide custom behavior when the environment's traits change. */
- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection API_AVAILABLE(ios(8.0));
/* Return whether this trait collection, compared to a different trait collection, could show a different appearance* for dynamic colors that are provided by UIKit or are in an asset catalog.* If you need to be aware of when dynamic colors might change, override `traitCollectionDidChange` in your view or view controller,* and use this method to compare `self.traitCollection` with `previousTraitCollection`.** Currently, a change in any of these traits could affect dynamic colors:*    userInterfaceIdiom, userInterfaceStyle, displayGamut, accessibilityContrast, userInterfaceLevel* and more could be added in the future.*/
- (BOOL)hasDifferentColorAppearanceComparedToTraitCollection:(nullable UITraitCollection *)traitCollection API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);

此方法进入后台时会调用2次,每次hasDifferentColorAppearanceComparedToTraitCollection都返回YES。stackoverflow也有此问题

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {[super traitCollectionDidChange:previousTraitCollection];if (@available(iOS 13.0, *)) {BOOL isDiffernt = [self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection];if (isDiffernt) {NSLog(@"%ld----------%ld", (long)self.traitCollection.userInterfaceStyle, previousTraitCollection.userInterfaceStyle);}} else {// Fallback on earlier versions}
}

暗黑模式(黑暗模式,深色模式)适配相关推荐

  1. myeclipse深色模式_完善深色模式的调色板

    myeclipse深色模式 Apps largely have a limited color palette which may already map well to dark mode. How ...

  2. Android深色模式适配原理分析,android应用开发

    return when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) { Configuration.UI ...

  3. iOS13适配深色模式(Dark Mode)

    原文博客地址: iOS13适配深色模式(Dark Mode) 好像大概也许是一年前, Mac OS系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的 终于, 随着iPhone11等新手机的 ...

  4. iOS13适配深色模式(Dark Mode)总结

    iOS13适配深色模式(Dark Mode)总结 好像大概也许是一年前, Mac OS系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的 终于,随着iPhone11等新手机的发售, iOS ...

  5. Android切换深色模式导致布局字体变小的解决方案

    切换深色模式导致布局字体变小问题困扰了我很久,一直排查自身代码问题却没发现并非自身代码导致,而是使用了今日头条屏幕适配方案AndroidAutoSize导致的,目前暂时在小米手机安卓11系统发现,切换 ...

  6. 【Web技术】623- 简单好用的前端深色模式/主题化开发方案

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师. 官方网站:devui.design  Ng组件库:ng-devui( ...

  7. Win10深色模式和白天模式设置

    在<最新Win10系统自动开启/关闭夜间模式的设置方法>一文中,介绍了windows 10系统自动开启/关闭夜间模式的设置方法.但windows 10系统还提供了 深色模式,在深色模式下, ...

  8. vue + element-UI 实现深色模式和主题色动态切换

    vue + element-UI 实现深色模式和主题色动态切换 文章目录 vue + element-UI 实现深色模式和主题色动态切换 前言 深色模式和主题色动态切换 本地样式切换 主题色切换 el ...

  9. C#+WPF+MaterialDesign图片瀑布流 和 一键深色模式

    加入MaterialDesign App.xaml <ResourceDictionary><ResourceDictionary.MergedDictionaries>< ...

  10. macOS Mojave 深色模式

    macOS Mojave 深色模式 mac 关闭 深色模式 https://support.apple.com/zh-cn/HT208976 https://www.apple.com/cn/maco ...

最新文章

  1. Windows Server 2008 R2 如何显示被隐藏的文件扩展名
  2. 服务器运行jupyter notebook,解决办法
  3. asynchttpclient 超时_DNF:95更新前还能免费获得一件超时空装备?但这个任务一定完成...
  4. zabbix监控TCP连接数
  5. OpenSSL常见用法
  6. PHP - ReflectorClass 反射类的作用
  7. 喜欢《权利的游戏》就一定要看《代码的游戏》
  8. 纽微特纪事:改个字串,竟然成了“二期工作”,还拖了几个月
  9. 制作课件的软件及特点作用
  10. 聚观早报 | 华为官宣新机Pocket S;苹果旗舰店开售智能门锁
  11. 纯js浏览器h5调用摄像头扫描识别解析 条形码+二维码
  12. 电影推荐算法及python实现
  13. python实现鼠标自动点击屏幕
  14. PX4-固定翼的姿态控制
  15. Sql Sugar使用仓储实现增删改查
  16. recover 没有捕获异常_Recover捕获异常
  17. P2440 木材加工(二分答案)
  18. npm i 命令安装失败提示:npm WARN read-shrinkwrap,解决方法
  19. cv::Mat转换为QImage错误
  20. pip安装tensorflow报错:could not find a version that satisfies the requirement grpcio=1.8.6

热门文章

  1. 自动缩放文字的TextView
  2. 按照日期排序相册库(支持自定义选中图片,视频数量,支持预览,支持拍摄仿小米原生相册)
  3. 电子病历的基本功能——主要功能hiscookbook.电子病历的基本功能
  4. Django7-4.模板系统常用知识点(4)
  5. 3D点云配准算法-4PCS(4点全等集配准算法)
  6. 搭建LEON3研发环境(一)
  7. 安全意识培训:如何提高员工网络安全意识?
  8. M1芯片Mac mini外接显示器的各种问题
  9. HSPC氢化大豆磷脂酰胆碱CAS92128-87-5
  10. 美团面试常见问题总结