如何在模拟器 iOS 13 中使用暗模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/57988687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:56:21  来源:igfitidea点击:

How to use dark mode in simulator iOS 13?

iosios-simulatorios-darkmode

提问by Mr.Javed Multani

While I am developing the iOS app I need to test it in simulator with dark mode option so I can get more clarity about the app UI. But when I go to the Setting I am not getting option for dark mode as real device showing.

在开发 iOS 应用程序时,我需要在模拟器中使用暗模式选项对其进行测试,以便我可以更清楚地了解应用程序 UI。但是当我进入设置时,我没有像真实设备显示那样获得暗模式选项。

回答by Ashley Mills

In Settings, scroll down to Developerand then Dark Appearance

Settings 中,向下滚动到Developer,然后是Dark Appearance...

enter image description here

在此处输入图片说明

回答by Ash Cameron

You can toggle the interface mode (i.e. Light / Dark) as well as adjust dynamic type setting on the fly (when the simulator is running) like this:

您可以切换界面模式(即亮/暗)以及动态调整动态类型设置(当模拟器运行时),如下所示:

Dark mode with Xcode

使用 Xcode 的深色模式

回答by blackjacx

Automated Appearance Change

自动外观更改

Xcode 11.4

Xcode 11.4

Apple released a powerful Xcode update which contains some useful things for this topic. Appearance can now be selected directly in the simulator. Just select Features > Toggle Appearanceor ? + ? + A. The automatic appearance switching could stil be important, e.g. for automated screenshot tests on the command line. The script becomes easy like this when using Xcode 11.4:

Apple 发布了一个强大的 Xcode 更新,其中包含有关此主题的一些有用内容。现在可以直接在模拟器中选择外观。只是选择Features > Toggle Appearance或?+ ? + A. 自动外观切换仍然很重要,例如对于命令行上的自动屏幕截图测试。使用 Xcode 11.4 时,脚本变得如此简单:

device_ids=("C741F3CD-FDAC-49EA-A4DB-7F797B97291E" "428183B6-3EB8-4D36-9938-9D07C141BF49")

# The value to use is now just 'dark' or 'light'
appearance=dark

for device in "${device_ids[@]}"; do
  xcrun simctl boot $device
  xcrun simctl ui $device appearance $appearance
done

No more simulator killing or re-setting needed which is a huge performance win. Also no fiddling around with any plist tools anymore - ever. keep in mind that you have to use device ids that are available in the runtime you use. Find them out using xcrun simctl list.

不再需要杀死模拟器或重新设置,这是一个巨大的性能胜利。也不再摆弄任何 plist 工具 - 永远。请记住,您必须使用在您使用的运行时中可用的设备 ID。使用xcrun simctl list.

See also my complete script: https://github.com/Blackjacx/Scripts/blob/master/set-simulator-style.sh

另请参阅我的完整脚本:https: //github.com/Blackjacx/Scripts/blob/master/set-simulator-style.sh

Xcode 11.3

Xcode 11.3

There is a way using the command line to switch a simulator between light and dark mode. If you have an array with your device IDs you can do the following:

有一种方法可以使用命令行在明暗模式之间切换模拟器。如果您有一个包含设备 ID 的数组,您可以执行以下操作:

device_ids=("C741F3CD-FDAC-49EA-A4DB-7F797B97291E" "428183B6-3EB8-4D36-9938-9D07C141BF49")

# Determine the plist value for the desired style: "dark" -> 2 / "light" -> 1
style=2

for device_id in "${device_ids[@]}"; do
  plist_path="${HOME}/Library/Developer/CoreSimulator/Devices/${device_id}/data/Library/Preferences/com.apple.uikitservices.userInterfaceStyleMode.plist"  
  printf '\n%s' "Set style $style for device $device_id ($plist_path)"

  killall "Simulator"
  xcrun simctl shutdown booted
  xcrun simctl erase $device_id

  # Crate the plist since it might not be available after erase
  [[ ! -f "$plist_path" ]] && /usr/libexec/PlistBuddy -c "save" $plist_path  
  # Set the style mode
  plutil -replace UserInterfaceStyleMode -integer $style $plist_path
done

If you want to specify device names in your script - since device IDs are different on different machines - you can also easily find the id's of them using the following bash code:

如果您想在脚本中指定设备名称 - 因为设备 ID 在不同的机器上是不同的 - 您还可以使用以下 bash 代码轻松找到它们的 ID:

device_names=("iPhone SE" "iPhone 8" "iPhone 11 Pro" "iPhone 11 Pro Max")
device_ids=()

for name in "${device_names[@]}"; do
  id=$(xcrun simctl list --json | jq ".devices | .[] | .[] | select(.name == \"$name\") | .udid" | cut -d\" -f2)
  device_ids+=("$id")
done

printf '%s\n' "${device_ids[@]}"

回答by thisIsTheFoxe

Alternatively, you can also switch the appearance programmatically (docs):

或者,您也可以以编程方式切换外观 ( docs):

override func viewDidLoad() {
    super.viewDidLoad()
    #if DEBUG
    // change the appearance only while testing  
    overrideUserInterfaceStyle = .dark
    #endif
}

回答by Shredder2794

You can use the "Toggle Appearance" setting from the Simulator menu dropdown (Shift-Command-A):

您可以使用模拟器菜单下拉菜单中的“切换外观”设置(Shift-Command-A):

enter image description here

在此处输入图片说明

回答by Jayprakash Dubey

There are two ways to enable dark mode in Simulator. Note: Make sure that you're using iOS 13 simulator. X-D

在模拟器中启用暗模式有两种方法。注意:确保您使用的是 iOS 13 模拟器。XD

Solution 1:Change build settings

解决方案 1:更改构建设置

  1. Open Settingsapp
  2. Select Developer
  3. Enable Dark appearance
  1. 打开设置应用
  2. 选择开发商
  3. 启用深色外观

Screenshot-1

截图 1

Solution 2:Programmatically

解决方案 2:以编程方式

Simply add this code block in your ViewController file.

只需将此代码块添加到您的 ViewController 文件中。

override func viewDidLoad() {
    super.viewDidLoad()
    #if DEBUG
    // This changes appearance only for debug mode
    overrideUserInterfaceStyle = .dark
    #endif
}

Screenshot-2

截图 2

Check this apple docsfor more details.

查看此Apple 文档以获取更多详细信息。

回答by Dhaval Gevariya

You can also switch Dark mode using this line, you just need to put these lines in your info.plist file

您也可以使用此行切换暗模式,您只需将这些行放在 info.plist 文件中

<key>UIUserInterfaceStyle</key>
<string>Dark</string> //Light