gpt4 book ai didi

ios - 在 swift 4 中使用 webkit 在 webView 中导航时创建事件指示器

转载 作者:行者123 更新时间:2023-11-29 00:02:17 24 4
gpt4 key购买 nike

我想在我的 iOS 应用程序中创建一个 wkwebview,并且我想向它添加一个事件指示器。我希望当我们点击 webview 中的所有按钮或部分时,它会出现事件指示器。你能给我一些代码来做到这一点吗?这是我的代码。它只在开始加载网站时显示指示器,但当我们点击该过程时,指示器不出现。

import UIKit
import WebKit

class ViewController: UIViewController,WKNavigationDelegate,UIWebViewDelegate {

@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var webView: WKWebView!
@IBOutlet var containerView: UIView? = nil

override func viewDidLoad() {
super.viewDidLoad()

self.view.addSubview(self.activityIndicator)

self.view.addSubview(self.webView)

let url:URL = URL(string : "https://www.facebook.com/")!
let urlRequest : URLRequest = URLRequest(url: url)
webView.load(urlRequest)

//activity indicator
self.webView.addSubview(self.activityIndicator)
self.activityIndicator.startAnimating()
self.webView.navigationDelegate = self
self.activityIndicator.hidesWhenStopped = true

//refresh
webView.scrollView.bounces = true
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(ViewController.refreshWebView), for: UIControlEvents.valueChanged)
webView.scrollView.addSubview(refreshControl)
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear( animated )

let url:URL = URL(string : "https://www.facebook.com/")!
let urlRequest : URLRequest = URLRequest(url: url)
webView.load(urlRequest)
}

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

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

@objc func refreshWebView(sender: UIRefreshControl) {
print("refersh")
sender.endRefreshing()
}
}

最佳答案

下面的代码将有助于在导航栏中添加事件指示器。所以不需要创建任何 subview 。这适用于 Swift 4。

import Foundation
import UIKit
import WebKit

class ContactVC: UIViewController, WKNavigationDelegate {

var webView: WKWebView!
var activityIndicator: UIActivityIndicatorView!

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

activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
activityIndicator.hidesWhenStopped = true
let barButton = UIBarButtonItem(customView: activityIndicator)
self.navigationItem.setRightBarButton(barButton, animated: true)

}

override func viewDidLoad() {
super.viewDidLoad()

let url = URL(string: "https://www.facebook.com/")!
webView.load(URLRequest(url: url))
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func showActivityIndicator(show: Bool) {
if show {
// Start the loading animation
activityIndicator.startAnimating()
} else {
// Stop the loading animation
activityIndicator.stopAnimating()
}
}

//MARK:- WKNavigationDelegate
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Start to load")
showActivityIndicator(show: true)
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("Finish to load")
title = webView.title
showActivityIndicator(show: false)
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print(error.localizedDescription)
showActivityIndicator(show: false)
}

func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print(error.localizedDescription)
showActivityIndicator(show: false)
}
}

关于ios - 在 swift 4 中使用 webkit 在 webView 中导航时创建事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49189213/

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