【发布时间】:2020-02-18 18:29:12
【问题描述】:
我用的是图片,这张图片有浅色和深色UIUserInterfaceStyle两个版本,但是当深色模式开启时,我需要在一个UIViewController中使用图片浅色版本。我知道我可以使用tintColor,但我想知道是否有其他方法可以做到这一点。
【问题讨论】:
标签: ios swift ios13 ios-darkmode uiuserinterfacestyle
我用的是图片,这张图片有浅色和深色UIUserInterfaceStyle两个版本,但是当深色模式开启时,我需要在一个UIViewController中使用图片浅色版本。我知道我可以使用tintColor,但我想知道是否有其他方法可以做到这一点。
【问题讨论】:
标签: ios swift ios13 ios-darkmode uiuserinterfacestyle
您可以通过在 viewDidLoad 函数中设置 overrideUserInterfaceStyle 属性来选择退出特定视图控制器的暗模式。示例 sn-p 如下。
override func viewDidLoad() {
super.viewDidLoad()
// Always adopt a light interface style.
overrideUserInterfaceStyle = .light
}
要为您的图像视图选择暗模式,您可以进行如下设置。
imageview. overrideUserInterfaceStyle = .light
您可以通过以下链接进一步了解选择退出暗模式。希望对您有所帮助。
【讨论】:
Subramanian 的回答非常适合这个用例。
但是,我确实只需要不同样式的 UIImage,您可以执行以下操作:
// create a trait collection with `light` interface style
let traitCollection = UITraitCollection(userInterfaceStyle: .light)
// If you have an existing image, you can change it's style like this:
let lightImage = image.withConfiguration(traitCollection.imageConfiguration)
// Or if you load the image by name, you can do this:
let lightImage = UIImage(named: "imageName", in: nil, compatibleWith: traitCollection)
【讨论】: