您的位置:首页 > 移动开发 > Swift

swiftui模式_如何在SwiftUI中检测明暗模式

2020-07-26 11:51 1871 查看

swiftui模式

You can detect the change from light to dark mode (and vice versa) in iOS 13 with an environment key called

ColorScheme
.

您可以使用名为

ColorScheme
的环境键在iOS 13中检测从亮模式到暗模式的变化(反之亦然)。

ColorScheme
enumerates the setting options for dark and light mode. Use this value to adjust the colors within your app, or even change some other variables. For example, I modified a text to reflect the current iPhone mode (“Light Mode” or “Dark Mode”)

ColorScheme
列举了暗和亮模式的设置选项。 使用此值可以调整应用程序中的颜色,甚至更改其他一些变量。 例如,我修改了文本以反映当前的iPhone模式(“浅模式”或“暗模式”)

First, you need to create constant variables for your light and dark colors. You can either add them to your content view, create an extension or struct for them, or add the colors outside of structs and classes.

首先,您需要为浅色和深色创建常量。 您可以将它们添加到内容视图中,为其创建扩展或结构,也可以在结构和类之外添加颜色。

Now, you can add the

ColorScheme
variable to content view, and use it to adjust the view to your liking.

现在,您可以将

ColorScheme
变量添加到内容视图,并使用它根据您的喜好调整视图。

@Environment(\.colorScheme) var colorScheme

You can change a shape or a view background color, like the code below:

您可以更改形状或视图背景颜色,例如以下代码:

.background(colorScheme == .light ? bgColorLight: bgColorDark)

You can also change other variables, like the text below:

您还可以更改其他变量,例如以下文本:

Text(colorScheme == .light ? "Light Mode" : "Dark Mode")

You can get similar results to the image above the left with the following code snippet:

使用以下代码片段,您可以获得与左上方图像类似的结果:

For the full project code, click here.

有关完整的项目代码, 请单击此处

翻译自: https://medium.com/better-programming/how-to-detect-light-and-dark-modes-in-swiftui-eef21ba4d11d

swiftui模式

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐