我已经看过许多教程,但仍然无法实现拉取以刷新Webview。我发现的所有教程都没有更新得很快,或者不是针对Webview的。
抱歉,我是新来的。
这是我尝试过的最有前途的方法,它编译时没有错误,但是没有刷新功能
import UIKit
import WebKit
class ViewController:
UIViewController,UIWebViewDelegate,UIScrollViewDelegate {
@IBOutlet weak var webview: WKWebView!
@IBOutlet weak var cnerror: UIImageView!
@IBOutlet weak var cnretry: UIButton!
@IBAction func cnretry(_ sender: Any) {
if Reachability.isConnectedToNetwork(){
webview.isHidden = false
cnerror.isHidden = true
cnretry.isHidden = true
webview.reload()
}else{
webview.isHidden = true
cnerror.isHidden = false
cnretry.isHidden = false
}
}
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.url(forResource: "redir", withExtension: "html")
let request = URLRequest(url: url!)
let day_url = URL(string: "https://branch.zylas.net/")
let day_url_request = URLRequest(url: day_url!,
cachePolicy:NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10.0)
let day_webView = UIWebView()
webview.scrollView.delegate = self;
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if (scrollView.contentOffset.y < 0){
//reach top
print("Reach Top")
webview.reload()
}
}
day_webView.loadRequest(day_url_request)
webview.load(request)
if Reachability.isConnectedToNetwork(){
webview.isHidden = false
}else{
webview.isHidden = true
cnerror.isHidden = false
cnretry.isHidden = false
}
}
override var prefersStatusBarHidden: Bool {
return true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
}
答案 0 :(得分:2)
@IBOutlet var webView: UIWebView!
var refController:UIRefreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
let url = URL.init(string: "https://www.google.com")
let requestObj = URLRequest.init(url: url!)
webView.loadRequest(requestObj);
UserDefaults.standard.register(defaults: ["UserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/601.5.17 (KHTML, like Gecko) Version/9.1 Safari/601.5.17"])
refController.bounds = CGRect.init(x: 0.0, y: 50.0, width: refController.bounds.size.width, height: refController.bounds.size.height)
refController.addTarget(self, action: #selector(self.mymethodforref(refresh:)), for: .valueChanged)
refController.attributedTitle = NSAttributedString(string: "Pull to refresh")
webView.scrollView.addSubview(refController)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@objc func mymethodforref(refresh:UIRefreshControl){
webView.reload()
refController.endRefreshing()
}
func webViewDidFinishLoad(webView: UIWebView) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
func webViewDidStartLoad(webView: UIWebView) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
尝试这段代码,我已经实现了UIrefreshController以实现拉动刷新