简体   繁体   English

iOS深色模式的自定义提升背景颜色

[英]Custom elevated background color for iOS dark mode

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.

相关问题 自定义暗模式 iOS 问题 - Custom Dark Mode iOS Issue iOS 13 UIBarButtonItem 在深色模式下的颜色 - iOS 13 UIBarButtonItem color in dark mode Objective-C - 以编程方式更改渐变背景颜色 UIViewController 当 iOS 13 更改暗模式时 - Objective-C - Change programmatically Gradient Background Color UIViewController When iOS 13 Dark Mode changed SFSafariViewController 没有带有自定义条形颜色的暗模式 - SFSafariViewController no dark mode with custom bar tint color SwiftUI:获取动态背景颜色(暗模式或亮模式) - SwiftUI: Get the Dynamic Background Color (Dark Mode or Light Mode) 具有系统背景颜色的列表在暗模式下变得混乱 - List with system background color gets messed up in dark mode 为什么我的键盘在深色模式下采用这种背景颜色? - Why my keyboard take this background color on Dark mode? SwiftUI 视图背景中的 UITextView 和暗模式(表单)的边框颜色 - UITextView inside a SwiftUI view background and border color for dark mode (Form) iOS 13暗模式大标题颜色怎么设置? - How to set iOS 13 dark mode large title color? 离子:无法在暗模式下更改 iOS 状态栏颜色 - Ionic: cannot change iOS status bar color in dark mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM