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 字体,使用 UIWebView 在 iOS 上无法正确显示

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 TitlesscrollEdgeAppearance。这两个属性处理自动明暗偏好更改以及半透明和垂直颜色渐变。我相信 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 颜色调整,因此仍然是在黑暗中拍摄?!

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

解决方法

我观察到 SFSafariViewController 正确地尊重了 UIColor 设置为 preferredBarTintColor 的明暗变体。您在 UIColor 扩展中对 teal025 的定义定义了单一颜色变体 (0x008080),并且没有单独的暗模式颜色。

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

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

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

或者,使用 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,alpha: 1)
        }
    }
}

最后,在更好地理解您的问题之后,您可能会发现 SFSafariViewController 条形颜色看起来与您的应用不同,因为条形半透明。目前无法访问 UINavigationBarSFSafariViewController 以禁用半透明。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...