簡體   English   中英

SFSafariViewController 沒有帶有自定義條形顏色的暗模式

[英]SFSafariViewController no dark mode with custom bar tint color

我從我的應用程序調用SFSafariViewController來打開myurl 我更改了SFSafariViewController的條形色調顏色。 這很好用:

vc.preferredBarTintColor = UIColor.teal025

但是,當設備將外觀樣式模式從淺色更改為深色時,條形色調顏色保持為teal025 ,並且不會調整為較暗的顏色,如果未使用preferredBarTintColor設置默認條形色調顏色(例如淺灰色到深灰色)。

該應用程序的主要部分也使用teal025作為導航欄色調。 這些 object 在進入暗模式時會根據需要自動調整顏色。

我怎樣才能對 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 fonts,在使用UIWebView的 Z1BDF605991920 上無法正確顯示。

NB2: teal025teal100是定制的colors。

--- 更新 --- (10.01.2021)

根據要求,在這里我如何定義(d)我的 colors。

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 顏色調整必須手動完成,因此仍然是在黑暗中拍攝?!

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

我觀察到SFSafariViewController正確地尊重UIColor設置為preferredBarTintColor的明暗變體。 您在 UIColor 擴展中定義的teal025定義了單一顏色變體(0x008080),並且沒有單獨的暗模式顏色。

要解決在顏色資產目錄中定義您的teal025並使用以下方法加載值:

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

確保通過為設置Appearance選擇“ Any,Dark ”來定義顏色資產中的深色和淺色變體,然后為每個設置適當的 colors。

或者,使用 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條形圖顏色看起來與您的應用程序不同,因為條形圖是半透明的。 目前沒有辦法通過訪問SFSafariViewControllerUINavigationBar來禁用半透明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM