简体   繁体   English

从应用程序打开系统暗模式 - Swift

[英]Turn on system dark mode from app - Swift

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.我已经使用Notifications在我的应用程序上创建了自己的暗模式系统,并且我有一个可以在暗模式打开和关闭之间切换的开关。

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 13,整个手机也会进入暗模式。

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?我的第二个问题:如何检查是否启用了系统暗模式,以便在启用 iOS 系统暗模式时,我可以在启用暗模式的地方进行设置?

  • First question: Impossible at this moment第一个问题:目前不可能
  • Second Question: Answer of Tamás Sengel第二个问题: Tamás Sengel 的回答

You should check the userInterfaceStyle variable of UITraitCollection , same as on tvOS and macOS.您应该检查UITraitCollectionuserInterfaceStyle变量,与 tvOS 和 macOS 相同。

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

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).您应该使用UIView / UIViewControllertraitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)函数来检测界面环境的变化(包括用户界面风格的变化)。

From Apple Developer Documentation :来自Apple 开发者文档

The system calls this method when the iOS interface environment changes.当iOS界面环境发生变化时,系统会调用该方法。 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.例如,当 iPhone 从纵向旋转到横向时,您可能会调整视图控制器子视图的布局。 The default implementation of this method is empty.此方法的默认实现为空。

System default UI elements (such as UITabBar or UISearchBar ) automatically adapt to the new user interface style.系统默认 UI 元素(例如UITabBarUISearchBar )会自动适应新的用户界面样式。

  if #available(iOS 13.0, *) {
            overrideUserInterfaceStyle = .light // or .dark 
        } else {
            // Fallback on earlier versions
        }

with this method you will get the light mode and dark mode, put this code into switch and your view will changes according to dark and light appearance.使用此方法您将获得亮模式和暗模式,将此代码放入开关中,您的视图将根据暗和亮的外观而变化。

  • First question: In AppDelegate第一个问题:在 AppDelegate 中

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let darkModeStatus = UserDefaults.standard.bool(forKey: "darkModeStatus") if darkModeStatus == true { window?.overrideUserInterfaceStyle = .dark } else { window?.overrideUserInterfaceStyle = .light } return true }
  • Second Question: with Toggle Switch in VC第二个问题:在VC中使用Toggle Switch

     @IBAction func darkModeSwitchClicked(_ sender: Any) { if darkModeToggleSwitch.isOn { UserDefaults.standard.set(true, forKey: "darkModeStatus") } else { UserDefaults.standard.set(false, forKey: "darkModeStatus") } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM