如何检测 iOS 13 中的 LightDark 模式变化? [英] How to detect LightDark mode change in iOS 13?

查看:31
本文介绍了如何检测 iOS 13 中的 LightDark 模式变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些 UI 设置无法在暗/亮模式更改为 UIColor 时自动工作.例如 shadow 在图层中.因为我需要在明暗模式下删除和放置阴影,所以我需要在某个地方放置 updateShadowIfNeeded() 函数.我知道如何检测当前的模式:

func dropShadowIfNeeded() {切换 traitCollection.userInterfaceStyle {案例.dark:removeShadow()case .light: dropShadowIfNotDroppedYet()默认值: assertionFailure("Unknown userInterfaceStyle")}}

现在我把这个函数放在 layoutSubviews 中,因为每次外观改变时它都会被调用:

override func layoutSubviews() {super.layoutSubviews()dropShadowIfNeeded()}

但是这个函数被调用很多.仅当 userInterfaceStyle 更改时才触发的正确功能是什么?

解决方案

SwiftUI

.colorScheme 键上使用一个简单的环境变量:

struct ContentView:查看{@Environment(.colorScheme) var colorSchemevar主体:一些视图{文本(colorScheme == .dark ?Its Dark":Its. not dark!(Light)")}}


UIKit

正如

灰色:打电话但不适合我的问题,绿色:为此而设计

所以我应该调用它并在这个函数中检查它:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {super.traitCollectionDidChange(previousTraitCollection)如果 traitCollection.hasDifferentColorAppearance(比较:previousTraitCollection){dropShadowIfNeeded()}}

这将保证每次更改只调用一次.

如果您只是在寻找样式的初始状态,在此处查看此答案

Some of the UI setups not working automatically with the Dark/Light mode change as the UIColor. For example shadow in layer. As I need to remove and drop shadow in dark and light mode, I need somewhere to put updateShadowIfNeeded() function. I know how to detect what is the mode currently:

func dropShadowIfNeeded() {
    switch traitCollection.userInterfaceStyle {
    case .dark: removeShadow()
    case .light: dropShadowIfNotDroppedYet()
    default: assertionFailure("Unknown userInterfaceStyle")
    }
}

Now I put the function inside the layoutSubviews, since it gets called every time appearance change:

override func layoutSubviews() {
    super.layoutSubviews()
    dropShadowIfNeeded()
}

But this function is getting called A LOT. What is the proper function to trigger only if userInterfaceStyle changed?

解决方案

SwiftUI

With a simple environment variable on the .colorScheme key:

struct ContentView: View {
    @Environment(.colorScheme) var colorScheme

    var body: some View {
        Text(colorScheme == .dark ? "Its Dark" : "Its. not dark! (Light)")
    }
}


UIKit

As it described in WWDC 2019 - Session 214 around 23:30.

As I expected, this function is getting called a lot including when colors changing. Along side with many other functions for ViewController and presentationController. But there is some especial function designed for that has a similar signature in all View representers.

Take a look at this image from that session:

Gray: Calling but not good for my issue, Green: Designed for this

So I should call it and check it inside this function:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)
    
    if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
        dropShadowIfNeeded()
    }
}

This will guarantee to be called just once per change.

if you are only looking for the initial state of the style, check out this answer here

这篇关于如何检测 iOS 13 中的 LightDark 模式变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆