首页 > 解决方案 > SFSafariViewController 没有带有自定义条形颜色的暗模式

问题描述

SFSafariViewController从我的应用程序调用打开myurl. 我改变了条形图的颜色SFSafariViewController。这很好用:

vc.preferredBarTintColor = UIColor.teal025

但是,当设备将外观样式模式从浅色更改为深色时,条形色调颜色保持为teal025并且不会调整为较暗的颜色,如果未设置使用默认条形色调颜色(例如,浅灰色到深灰色)preferredBarTintColor

该应用程序的主要部分也使用teal025作为导航栏色调。进入暗模式时,这些对象会根据需要自动调整颜色。

我怎样才能对 SFSafariViewController 栏色调颜色有相同的行为?

这里是整个代码:

let urlString = "https://myurl"
if let url = URL(string: urlString) {
    let vc = SFSafariViewController(url: url)
    vc.preferredBarTintColor = UIColor.teal025 // this prevents dark mode change
    vc.preferredControlTintColor = UIColor.teal100
    present(vc, animated: true)
}

NB1:我不想使用UIWebView,因为 myurl 使用 Google 字体,在 iOS 上使用UIWebView.

NB2:teal025teal100是自定义颜色。

--- 更新 --- (10.01.2021)

根据要求,在这里我如何定义(d)我的颜色。

extension UIColor {
    static var teal025: UIColor { return UIColor(red: 0.0, green: 127.0/255.0, blue: 127.0/255.0, alpha: 1.0) } // 0x008080
}

--- 更新 --- (12.01.2021)

我的目标是拥有一个 SFSafariViewController 栏色调,它与应用程序的导航栏具有完全相同的色调、渐变和半透明度。我的导航是类型Prefer Large Titles样式以及scrollEdgeAppearance. 这两个属性处理自动亮/暗偏好变化以及半透明和垂直颜色渐变。我相信 SFSafariViewController 没有所有这些规定。所以最接近的是 UIColor 的 dynamicProvider 初始化器,正如 CSmith 所建议的那样。这将“仅”解决明/暗偏好变化。

let urlString = "https://myurl"
if let url = URL(string: urlString) {
    let vc = SFSafariViewController(url: url)
    vc.preferredBarTintColor = UIColor.safariBarTint
    vc.preferredControlTintColor = UIColor.safariControlTint
    present(vc, animated: true)
}

extension UIColor {
    struct Material {
        static var orangeA100: UIColor { return UIColor(red: 0xff / 0xff,
                                                        green: 0xd1 / 0xff,
                                                        blue:  0x80 / 0xff,
                                                        alpha: 1.0) } // #FFD180
        ...
        static var orangeA700: UIColor { return UIColor(red: 0xff / 0xff,
                                                        green: 0x6d / 0xff,
                                                        blue:  0x00 / 0xff,
                                                        alpha: 1.0) } // #FF6D00
    }
}

static var safariBarTint: UIColor {
    if #available(iOS 13.0, *) {
        return UIColor { (traits: UITraitCollection) -> UIColor in
            return traits.userInterfaceStyle == .dark ?
                UIColor.Material.orangeA700 :
                UIColor.Material.orangeA100
        }
    } else { // for iOS 12 and earlier
        return UIColor.Material.orangeA100
    }
}
static var safariControlTint: UIColor {
    if #available(iOS 13.0, *) {
        return UIColor { (traits: UITraitCollection) -> UIColor in
            return traits.userInterfaceStyle == .dark ?
                UIColor.Material.orangeA100 :
                UIColor.Material.orangeA700
        }
    } else { // for iOS 12 and earlier
        return UIColor.Material.orangeA700
    }
}

我相信应用程序的导航栏和 SFSafariViewController 之间的 1:1 颜色调整必须手动完成,因此仍然是在黑暗中拍摄?!

我相信上面的代码是我能得到的最接近的。

标签: iosswiftsfsafariviewcontrollerios-darkmode

解决方案


我观察到SFSafariViewController正确尊重集合的明暗变体UIColorpreferredBarTintColor. 您在 UIColor 扩展中定义的teal025定义了一个单一的颜色变体(0x008080),并且没有单独的暗模式颜色。

要解决在颜色资产目录中定义您的teal025并使用以下方法加载值:

preferredBarTintColor = UIColor.init(named:"teal025")

确保通过为设置选择“ Any,Dark ”来定义颜色资源中的深色和浅色变体Appearance,然后为每种颜色设置适当的颜色。

或者,使用 UIColor 的 dynamicProvider 初始化程序在运行时返回暗模式和亮模式变体,如下所示:

extension UIColor 
{
    static var teal025: UIColor 
    {
        if #available(iOS 13.0, *) 
        {
            return UIColor { (traits: UITraitCollection) -> UIColor in
                
                // Return one of two colors depending on light or dark mode
                return traits.userInterfaceStyle == .dark ?
                    UIColor(red: 0.0, green: 0.5, blue: 0.5, alpha: 1) :
                    UIColor(red: 0.0, green: 0.4, blue: 0.4, alpha: 1)
            }
        } 
        else 
        {
            // for iOS 12 and earlier
            return UIColor(red: 0.0, green: 0.5, blue: 0.5, alpha: 1)
        }
    }
}

最后,在更好地理解了您的问题之后,您可能会发现SFSafariViewController由于条形半透明,条形颜色看起来与您的应用程序不同。目前没有任何方法可以访问UINavigationBarSFSafariViewController禁用半透明。


推荐阅读