问题
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.
回答1:
In Settings, scroll down to Developer and then Dark Appearance…

回答2:
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:
回答3:
Alternatively, you can also switch the appearance programmatically (docs):
override func viewDidLoad() {
super.viewDidLoad()
#if DEBUG
// change the appearance only while testing
overrideUserInterfaceStyle = .dark
#endif
}
回答4:
Automated Dark / Light Change
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:
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:
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[@]}"`
来源:https://stackoverflow.com/questions/57988687/how-to-use-dark-mode-in-simulator-ios-13