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

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")
Image for post

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模式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值