WKWebview가로드 될 때 채워지는 진행률 표시 줄을 추가하려고합니다. 아래 코드를 사용해 보았습니다.
UIKit 가져오기WebKit 가져오기GmailViewController class : UIViewController, WKUIDelegate, WKNavigationDelegate {
@IBOutlet weak var progressView: UIProgressView!
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var webView: WKWebView!
@IBAction func homeBtn(_ sender: Any) {
self.performSegue(withIdentifier: "HomeScreen", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
webConfiguration.websiteDataStore = WKWebsiteDataStore.default()
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.navigationDelegate = self
webView.uiDelegate = self
self.webView = webView
if let webView = self.webView
{
view.addSubview(webView)
webView.translatesAutoresizingMaskIntoConstraints = false
let height = NSLayoutConstraint(item: webView, attribute: .height, relatedBy: .equal, toItem: self.contentView, attribute: .height, multiplier: 1, constant: 0)
let width = NSLayoutConstraint(item: webView, attribute: .width, relatedBy: .equal, toItem: self.contentView, attribute: .width, multiplier: 1, constant: 0)
let offset = NSLayoutConstraint(item: webView, attribute: .top, relatedBy: .equal, toItem: self.contentView, attribute: .top, multiplier: 1, constant: 0)
view.addConstraints([height, width, offset])
}
let myURL = URL(string: "https://gmail.com/mail")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
progressView = UIProgressView(progressViewStyle: .default)
progressView.sizeToFit()
// Set frame to exact below of navigation bar if available
progressView.frame = CGRect(x: 0, y: 64, width: self.view.bounds.size.width, height: 20)
self.view.addSubview(progressView)
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" {
print("estimatedProgress")
progressView.progress = Float(webView.estimatedProgress)
}}
}
이상한 이유로 runtime에 오류를 반환합니다. nil에 대한 이벤트를 설정할 수 없습니다.