[英]Custom elevated background color for iOS dark mode
In the dark mode section of the Human Interface Guidelines<\/a> , Apple describes that there is basically three background colors when you use the system background - light, dark, and dark elevated (which is used for modals, for example).在
Human Interface Guidelines 的深色模式部分<\/a>,Apple 描述了当您使用系统背景时基本上存在三种背景颜色 - 浅色、深色和深色提升(例如,用于模态)。
Thanks Kurt Revis for pointing my to this .感谢 Kurt Revis 向我指出这一点。 I was able to do this using a special UIColor initializer that Swift provides.
我可以使用 Swift 提供的特殊 UIColor 初始化程序来做到这一点。 The only downside is that this will not work in interface builder (since it is not baked into the Asset itself), but in code this will work perfectly:
唯一的缺点是这在界面生成器中不起作用(因为它没有融入资产本身),但在代码中这将完美地工作:
class ElevatedColor: UIColor {
convenience init(regular: UIColor, elevated: UIColor) {
if #available(iOS 13.0, *) {
self.init { (traitCollection: UITraitCollection) -> UIColor in
let isElevated = (traitCollection.userInterfaceLevel == .elevated)
return isElevated ? elevated : regular
}
} else {
self.init(cgColor: regular.cgColor)
}
}
}
This uses UIColor.init(dynamicProvider:)
.这使用
UIColor.init(dynamicProvider:)
。 iOS will call the provided block whenever the interface traits change, so the color automatically updates when switching to an elevated context.每当界面特征发生变化时,iOS 都会调用提供的块,因此在切换到提升的上下文时颜色会自动更新。 Unfortunately, because of the way UIColor works , you can only make the initializer a convenience initializer, so technically you could create an ElevatedColor without an elevated version, but for me this is acceptable.
不幸的是,由于UIColor 的工作方式,您只能使初始化程序成为便利初始化程序,因此从技术上讲,您可以在没有提升版本的情况下创建 ElevatedColor,但对我来说这是可以接受的。
Firstly, you cannot USE<\/strong> the elevated style.首先,您不能使用<\/strong>提升的样式。 It's reserved for system functions such as when you go to the multi-task menu, or when you open a modal view.
它是为系统功能保留的,例如当您进入多任务菜单或打开模式视图时。
The Apple HIGs state that Apple HIG 声明
Dark Mode is dynamic, which means that the background color automatically<\/strong> changes from base to elevated...暗模式是动态的,这意味着背景颜色会自动<\/strong>从基础颜色变为高光...
<\/blockquote>
emphasis on "automatically", and强调“自动”,并且
Using a custom background color can make it harder for people to perceive these system-provided visual distinctions使用自定义背景颜色会使人们更难感知这些系统提供的视觉区别
<\/blockquote>
So I believe, that using a custom color will not dynamically apply elevated style to your background.所以我相信,使用自定义颜色不会动态地将提升的样式应用于您的背景。 You can only use
Light<\/code> or
dark<\/code> as of now in swift
到目前为止,您只能在 swift 中使用
Light<\/code>或
dark<\/code>
"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.