gpt4 book ai didi

iOS UIActivityIndi​​cator View 未出现在 WKWebKit View 中

转载 作者:行者123 更新时间:2023-11-29 11:31:07 25 4
gpt4 key购买 nike

当我第一次运行应用程序并且网页正在加载时,加载指示器按预期显示。

然后它会在页面加载时按预期消失。

然后我导航到一个新网页,但指示器没有再次出现

有什么建议吗?

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()
}
}

最佳答案

请检查下面的代码。你犯的错误是没有更新到父类(super class)意味着 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()
}
}

关于iOS UIActivityIndi​​cator View 未出现在 WKWebKit View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53500071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com