从应用程序打开系统暗模式-Swift [英] Turn on system dark mode from app - Swift

查看:52
本文介绍了从应用程序打开系统暗模式-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Notifications 在我的应用上制作了自己的暗模式系统,并且我有一个在暗模式打开和关闭之间切换的开关.

I have made my own system of dark mode on my app using Notifications and I have a switch that changes between dark mode on and off.

我的第一个问题:如何打开系统暗模式,如果通过将开关更新到iOS 13,整个手机也将变为暗模式.

My first question: How do I turn on system dark mode, where the whole phone becomes dark mode as well if it is updated to iOS 13 by flipping the switch.

我的第二个问题:如何检查是否启用了系统暗模式,以便每当启用iOS系统暗模式时都可以在启用暗模式的地方进行设置?

My second question: How do I check to see if system dark mode is enabled so that I can make it where my dark mode is enabled whenever the iOS system dark mode is enabled?

推荐答案

  • 第一个问题:目前无法实现
  • 第二个问题:TamásSengel 答案

    • First question: Impossible at this moment
    • Second Question: Answer of Tamás Sengel
    • 您应该检查 UITraitCollection userInterfaceStyle 变量,与在tvOS和macOS上相同.

      You should check the userInterfaceStyle variable of UITraitCollection, same as on tvOS and macOS.

      switch traitCollection.userInterfaceStyle {
      case .light: //light mode
      case .dark: //dark mode
      case .unspecified: //the user interface style is not specified
      }
      

      您应该使用 traitCollectionDidChange(_ previousTraitCollection:UITraitCollection?) UIView / UIViewController 函数可检测界面环境中的更改(包括用户界面样式的更改).

      You should use the traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) function of UIView/UIViewController to detect changes in the interface environment (including changes in the user interface style).

      来自 Apple开发者文档:

      当iOS界面环境更改时,系统调用此方法.根据您的应用需求,在视图控制器和视图中实施此方法,以响应此类更改.例如,当iPhone从纵向旋转为横向时,您可以调整视图控制器的子视图的布局.此方法的默认实现为空.

      The system calls this method when the iOS interface environment changes. Implement this method in view controllers and views, according to your app’s needs, to respond to such changes. For example, you might adjust the layout of the subviews of a view controller when an iPhone is rotated from portrait to landscape orientation. The default implementation of this method is empty.

      系统默认的UI元素(例如 UITabBar UISearchBar )会自动适应新的用户界面样式.

      System default UI elements (such as UITabBar or UISearchBar) automatically adapt to the new user interface style.

      这篇关于从应用程序打开系统暗模式-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆