gpt4 book ai didi

swift - 使用 WKWebView 以编程方式添加 ActivityIndi​​cator 时出现问题,未出现

转载 作者:行者123 更新时间:2023-11-30 10:36:12 24 4
gpt4 key购买 nike

我的代码成功加载了我想要的网页;但是,我在加载时添加事件指示器时遇到问题。

我对 swift 比较陌生,所以我正在阅读其他 stackexchange 示例。我创建了一个 UIActivityIndi​​cator,将其设置在覆盖函数中,但它没有显示。

网页加载正常。这是可运行的代码。

import UIKit
import WebKit

class FeedbackWebViewController: UIViewController, WKNavigationDelegate {

let indicator = UIActivityIndicatorView(style: .gray)

var webView: WKWebView!

override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self

indicator.center = webView.center
indicator.hidesWhenStopped = true
webView.addSubview(indicator)

view = webView
}
override func viewDidLoad() {
super.viewDidLoad()


let url = URL(string: "https://www.apple.com")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true

self.indicator.startAnimating()
}


func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
indicator.stopAnimating()
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
indicator.stopAnimating()
}
}

最佳答案

不要覆盖 loadView。可能会发生不好的事情。移动 didLoad 中的代码。它应该可以工作。

关于swift - 使用 WKWebView 以编程方式添加 ActivityIndi​​cator 时出现问题,未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57978984/

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