gpt4 book ai didi

xcode - 如何在 iOS Xcode Swift Playground 上模拟黑暗模式

转载 作者:行者123 更新时间:2023-12-05 06:21:09 25 4
gpt4 key购买 nike

类似于How to use dark mode in simulator iOS 13?How to enable Dark Mode on the Simulator? ,如何在 Xcode Swift Playground Live View 中启用深色模式?

最佳答案

您可以使用 overrideUserInterfaceStyle 在任何 View 中测试它

例如,您可以将这段代码粘贴到 playground 中进行检查(已在 Xcode 11.3.1 中测试):

import Foundation
import PlaygroundSupport

extension UIColor {

static var primary: UIColor {
UIColor { trait -> UIColor in
return trait.userInterfaceStyle == .light ? .black : .white
}
}

static var secondary: UIColor {
UIColor { trait -> UIColor in
return trait.userInterfaceStyle == .light ? .white : .black
}
}
}

class DarkModeTestView: UIView {

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 375, height: 300))

override init(frame: CGRect) {
super.init(frame: frame)

backgroundColor = .secondary

label.text = "This text should be displayed in black for light mode and white for dark mode"
label.textColor = .primary
label.numberOfLines = 0

addSubview(label)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

let testView = DarkModeTestView(frame: CGRect(x: 0, y: 0, width: 375, height: 300))
testView.overrideUserInterfaceStyle = .dark // Change here .light or .dark

PlaygroundPage.current.liveView = testView

关于xcode - 如何在 iOS Xcode Swift Playground 上模拟黑暗模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60079597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com