我想在MBProgressHUD
上展示UIWebView
。我正在尝试在webview中加载iframe。我的微调器在webview开始加载时显示,但即使它转到webView didFinishLoad()
中的断点也不会隐藏。
这是我的代码:
class LiveViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var liveWebView: UIWebView!
var spinnerActivity: MBProgressHUD! = nil
override func viewDidLoad() {
super.viewDidLoad()
liveWebView.delegate = self
let embededHTML = "<html><body><iframe src=\"\" width=\"100%\" height=\"500\" frameborder=\"0\" scrolling=\"no\"></iframe></body></html>" //link removed
liveWebView.loadHTMLString(embededHTML, baseURL: nil)
}
public func webViewDidStartLoad(_ webView: UIWebView){
spinnerActivity = MBProgressHUD.showAdded(to: self.view, animated: true)
spinnerActivity.label.text = "Loading"
spinnerActivity.detailsLabel.text = "Please Wait!"
spinnerActivity.isUserInteractionEnabled = false
}
public func webViewDidFinishLoad(_ webView: UIWebView){
spinnerActivity.hide(animated: true)
}
public func webView(_ webView: UIWebView, didFailLoadWithError error: Error){
spinnerActivity.hide(animated: true)
}
}
我注意到webViewDidStartLoad和webViewDidFinishLoad被调用了两次。 请帮我。微调器没有隐藏..我使用过Xcode8和Swift 3.
编辑:
我试过了,但它也不起作用:
class LiveViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var liveWebView: UIWebView!
var spinnerActivity: MBProgressHUD! = nil
override func viewDidLoad() {
super.viewDidLoad()
liveWebView.delegate = self
let embededHTML = "<html><body><iframe src=\"\" width=\"100%\" height=\"500\" frameborder=\"0\" scrolling=\"no\"></iframe></body></html>" //link removed
liveWebView.loadHTMLString(embededHTML, baseURL: nil)
}
public func webViewDidStartLoad(_ webView: UIWebView){
MBProgressHUD.showAdded(to: self.view, animated: true)
}
public func webViewDidFinishLoad(_ webView: UIWebView){
MBProgressHUD.hide(for: self.view, animated: true)
}
public func webView(_ webView: UIWebView, didFailLoadWithError error: Error){
MBProgressHUD.hide(for: self.view, animated: true)
}
}
请参阅
答案 0 :(得分:3)
尝试用此替换您的代码:
class LiveViewController: UIViewController, UIWebViewDelegate {
var isHudShow = false
@IBOutlet weak var liveWebView: UIWebView!
var spinnerActivity: MBProgressHUD! = nil
override func viewDidLoad() {
super.viewDidLoad()
liveWebView.delegate = self
let embededHTML = "<html><body><iframe src=\"\" width=\"100%\" height=\"500\" frameborder=\"0\" scrolling=\"no\"></iframe></body></html>" //link removed
liveWebView.loadHTMLString(embededHTML, baseURL: nil)
}
public func webViewDidStartLoad(_ webView: UIWebView){
if isHudShow == false
{
MBProgressHUD.showAdded(to: self.view, animated: true)
isHudShow =true
}
}
public func webViewDidFinishLoad(_ webView: UIWebView){
MBProgressHUD.hide(for: self.view, animated: true)
}
public func webView(_ webView: UIWebView, didFailLoadWithError error: Error){
MBProgressHUD.hide(for: self.view, animated: true)
}
}