【发布时间】:2021-01-07 10:42:03
【问题描述】:
我从我的应用程序调用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:teal025 和 teal100 是自定义颜色。
--- 更新 --- (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 颜色适配必须手动完成,因此仍然是在黑暗中拍摄?!
我相信上面的代码是我能得到的最接近的。
【问题讨论】:
标签: ios swift sfsafariviewcontroller ios-darkmode