向 WKWebView 添加加载栏并选择其颜色
Adding a loading bar to WKWebView and selecting its color
我正在尝试将加载栏添加到我的 WebView 页面的顶部。我将在哪里添加代码。也可以更改此加载栏的颜色吗?
class CycleViewController: UIViewController, WKUIDelegate {
var window: UIWindow?
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
webView.backgroundColor = UIColor.clear
webView.backgroundColor = UIColor(red: 0.0196, green: 0.4, blue: 0.2902, alpha: 1.0)
webView.isOpaque = false
let myURL = URL(string: "https://jwelsh19.wixsite.com/countryday")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
如果您不想要自定义视图,则可以使用 SFSafariViewController 来实现。使用下面的代码
if let url = URL(string: "https://jwelsh19.wixsite.com/countryday") {
let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = true
let vc = SFSafariViewController(url: url, configuration: config)
present(vc, animated: true)
}
如果您不想使用 SFSafariViewController,则将 uiprogress bar 添加到您的 webview containerview 并观察 webview 进度并相应地更新 UIProgressbar
为了观察 webview 进度,请使用 webview progress observer
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress" {
progressView.progress = Float(webView.estimatedProgress)
}
}
我正在尝试将加载栏添加到我的 WebView 页面的顶部。我将在哪里添加代码。也可以更改此加载栏的颜色吗?
class CycleViewController: UIViewController, WKUIDelegate {
var window: UIWindow?
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
webView.backgroundColor = UIColor.clear
webView.backgroundColor = UIColor(red: 0.0196, green: 0.4, blue: 0.2902, alpha: 1.0)
webView.isOpaque = false
let myURL = URL(string: "https://jwelsh19.wixsite.com/countryday")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
如果您不想要自定义视图,则可以使用 SFSafariViewController 来实现。使用下面的代码
if let url = URL(string: "https://jwelsh19.wixsite.com/countryday") {
let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = true
let vc = SFSafariViewController(url: url, configuration: config)
present(vc, animated: true)
}
如果您不想使用 SFSafariViewController,则将 uiprogress bar 添加到您的 webview containerview 并观察 webview 进度并相应地更新 UIProgressbar 为了观察 webview 进度,请使用 webview progress observer
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress" {
progressView.progress = Float(webView.estimatedProgress)
}
}