如何设置渐变颜色以在黑暗模式 ios 中查看? [英] How to set gradient color to view in Dark mode ios?

查看:45
本文介绍了如何设置渐变颜色以在黑暗模式 ios 中查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在深色模式下更改渐变颜色.但它没有改变.设置视图后设置渐变.在明暗模式之间切换时颜色不变.

I want to change the gradient color in dark mode. But it not changing. Gradient set once the view is setup. Color not changing while switching between light and dark mode.

 if let layer = layer as? CAGradientLayer {
        if let startGradientColor = startGradientColor, let endGradientColor = endGradientColor {
            layer.colors = [startGradientColor.cgColor, endGradientColor.cgColor]
        } else {
            layer.colors = gradientColors.map {$0.cgColor}
        }
        layer.startPoint = CGPoint(x: 0, y: 0)  // top
        layer.endPoint = CGPoint(x: 1, y: 1)    // bottom
    }

推荐答案

你需要让你的应用对 trait 集合的变化做出反应,就像这样:

You need to make your app react to changes to the trait collection, something like this:

extension YourViewController {
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)

        if #available(iOS 13.0, *) {
            guard traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) else {
                return
            }

            // redraw your layers here
        }
    }
}

这篇关于如何设置渐变颜色以在黑暗模式 ios 中查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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