简体   繁体   English

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

[英]How to use dark mode in simulator iOS 13?

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.在开发 iOS 应用程序时,我需要在模拟器中使用暗模式选项对其进行测试,以便我可以更清楚地了解应用程序用户界面。 But when I go to the Setting I am not getting option for dark mode as real device showing.但是当我 go 进入设置时,我没有像真实设备显示那样获得暗模式选项。

In Settings , scroll down to Developer and then Dark AppearanceSettings 中,向下滚动到Developer ,然后是Dark Appearance ...

在此处输入图片说明


Update更新

In addition to the above, there are now many other ways to enable dark appearance in the simulator, as shown in the many great answers below.除了上述方法之外,现在还有许多其他方法可以在模拟器中启用深色外观,如下面的许多很棒的答案所示。

• Change Environment Overrides from Xcode ( @AshCameron ) • 从 Xcode 更改环境覆盖 ( @AshCameron )

• Toggle Appearance A from the Simulator menu ( @Shredder2794 ) • 从模拟器菜单中切换外观 A ( @Shredder2794 )

• Update from the command line using xcrun simctl ui booted appearance … ( @blackjacx, @tadija ) • 使用xcrun simctl ui booted appearance …从命令行更新xcrun simctl ui booted appearance …@blackjacx、@tadija

• Programmatically using overrideUserInterfaceStyle = .dark ( @thisIsTheFoxe ) • 以编程方式使用overrideUserInterfaceStyle = .dark ( @thisIsTheFoxe )

• Specify UIUserInterfaceStyle in your info.plist (@ DhavalGevariya ) • 在info.plist 中指定UIUserInterfaceStyle (@ DhavalGevariya )

• Use SimGenie from Curtis Herbert… https://simgenie.app • 使用 Curtis Herbert 的 SimGenie... https://simgenie.app

You can toggle the interface mode (ie Light / Dark) as well as adjust dynamic type setting on the fly (when the simulator is running) like this:您可以切换界面模式(即亮/暗)以及动态调整动态类型设置(当模拟器运行时),如下所示:

使用 Xcode 的深色模式

You can use the "Toggle Appearance" ( Shift Command A ) setting from the Simulator menu:您可以使用 Simulator 菜单中的“Toggle Appearance”( Shift Command A )设置:

Simulator > Features > Toggle Appearance ⇧⌘A Simulator > Features > Toggle Appearance ⇧⌘A

切换外观

Automated Appearance Change 🦉自动外观更改🦉

Xcode 11.4 🌟 Xcode 11.4 🌟

Apple released a powerful Xcode update which contains some useful things for this topic. Apple 发布了一个强大的 Xcode 更新,其中包含有关此主题的一些有用内容。 Appearance can now be selected directly in the simulator.现在可以直接在模拟器中选择外观。 Just select Features > Toggle Appearance or + + A .只需选择Features > Toggle Appearance + + A The automatic appearance switching could stil be important, eg for automated screenshot tests on the command line.自动外观切换仍然很重要,例如对于命令行上的自动屏幕截图测试。 The script becomes easy like this when using Xcode 11.4:使用 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.也不再摆弄任何 plist 工具 - 永远。 keep in mind that you have to use device ids that are available in the runtime you use.请记住,您必须使用在您使用的运行时中可用的设备 ID。 Find them out using xcrun simctl list .使用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[@]}"

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
}

There are two ways to enable dark mode in Simulator.在模拟器中启用暗模式有两种方法。 Note: Make sure that you're using iOS 13 simulator.注意:确保您使用的是 iOS 13 模拟器。 XD XD

Solution 1: Change build settings解决方案 1:更改构建设置

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

截图 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
}

截图 2

Check this apple docs for more details.查看此Apple 文档以获取更多详细信息。

from terminal:从终端:

xcrun simctl ui booted appearance light

xcrun simctl ui booted appearance dark

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

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

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