当我第一次运行该应用程序且网页正在加载时,加载指示符将按预期显示。
然后在页面加载时按预期消失。
然后,我导航到一个新的网页,并且指示器没有重新出现
任何建议为什么要这么做?
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
var activityIndicator: UIActivityIndicatorView!
override func loadView() {
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
webView = WKWebView(frame: CGRect(x:0, y: 0, width:screenWidth, height:screenHeight))
webView.navigationDelegate = self
//webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.bbc.co.uk")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
activityIndicator = UIActivityIndicatorView()
activityIndicator.center = view.center
//activityIndicator.hidesWhenStopped = true
view.addSubview(activityIndicator)
}
override var prefersStatusBarHidden: Bool {
return true
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Page loading")
activityIndicator.startAnimating()
}
func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) {
print("Page loaded")
activityIndicator.stopAnimating()
}
}
答案 0 :(得分:1)
请检查下面的代码。您正在执行的错误是没有更新为超类意味着super.loadView()
。
import UIKit
import WebKit
class ViewController3: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
var activityIndicator: UIActivityIndicatorView!
override func loadView() {
super.loadView()
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
webView = WKWebView(frame: CGRect(x:0, y: 0, width:screenWidth, height:screenHeight))
webView.navigationDelegate = self
//webView.uiDelegate = self
view.addSubview(webView)
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
activityIndicator.center = view.center
//activityIndicator.style = .whiteLarge
activityIndicator.color = .red
activityIndicator.startAnimating()
//activityIndicator.hidesWhenStopped = true
view.addSubview(activityIndicator)
}
override var prefersStatusBarHidden: Bool {
return true
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Page loading")
activityIndicator.startAnimating()
}
func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) {
print("Page loaded")
activityIndicator.stopAnimating()
}
}