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中检测从亮模式到暗模式的变化(反之亦然)。
ColorSchemeenumerates 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
ColorSchemevariable 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模式
- 如何检测iPhone设备处于低电量模式
- 如何用C语言中的union检测CPU的端模式
- 关于windows10下如何检测系统现在是否为平板模式。
- Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)
- 如何用程序检测系统处理器的存储模式(即是大端存储还是小端存储)
- 如何检测cpu的字节顺序是小端模式还是大端模式
- 【AI技术生态论】FreeWheel孙大伟:AI如何改变视频广告的商业模式?
- 从拼团与社群模式,看垂直电商如何打破流量瓶颈?
- How does the Visitor Pattern come from?如何推导出访问者模式
- js如何检测用户有没有在操作
- 如何破解 Google Chrome 的隐身模式?
- 如何破解 Google Chrome 的隐身模式?
- 如何在linux下检测内存泄漏
- test() 方法用于检测一个字符串是否匹配某个模式.
- 如何进入Linux单用户模式
- linux下如何进入单用户模式
- 如何使用设计模式——qq交流群讨论纪实
- 中小中介公司,如何应对链家推出的贝壳找房那种VR看房模式
- WebBrowser与IE的关系,如何设置WebBrowser工作在IE9模式下?
- QQ/微信里被禁止访问的网页怎么处理 如何检测域名是否被微信封