我正在为我的IOS Webview应用程序使用自定义活动指示器(http://swiftonic.com/customized-activity-indicator-in-swift-3/)。它正在工作,但是我遇到的问题是页面加载时指示器没有隐藏或消失。动画将继续。这是我的代码
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet var myWebView: WKWebView!
//@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// In order to Show activityIndicatorView
ViewControllerUtils().showActivityIndicator(uiView: self.view)
// In order to hide activityIndicatorView
ViewControllerUtils().hideActivityIndicator(uiView: self.view)
let url = URL(string: "https://oasis.geneseo.edu")!
myWebView.navigationDelegate = self as? WKNavigationDelegate
myWebView.load(URLRequest(url: url))
}
根据指示ViewControllerUtils().showActivityIndicator(uiView: self.view)
,应显示指示符和ViewControllerUtils().showActivityIndicator(uiView: self.view)
。这是我将UIActivityIndicatorView
存储在单独文件中的代码。
import Foundation
import UIKit
class ViewControllerUtils {
var container: UIView = UIView()
var loadingView: UIView = UIView()
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
/*
Show customized activity indicator,
actually add activity indicator to passing view
@param uiView - add activity indicator to this view
*/
func showActivityIndicator(uiView: UIView) {
container.frame = uiView.frame
container.center = uiView.center
container.backgroundColor = UIColorFromHex(rgbValue: 0xffffff, alpha: 0.3)
loadingView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
loadingView.center = uiView.center
loadingView.backgroundColor = UIColorFromHex(rgbValue: 0x444444, alpha: 0.7)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 10
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50);
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
activityIndicator.center = CGPoint(x: loadingView.frame.size.width / 2, y: loadingView.frame.size.height / 2);
loadingView.addSubview(activityIndicator)
container.addSubview(loadingView)
uiView.addSubview(container)
activityIndicator.startAnimating()
}
/*
Hide activity indicator
Actually remove activity indicator from its super view
@param uiView - remove activity indicator from this view
*/
func hideActivityIndicator(uiView: UIView) {
activityIndicator.stopAnimating()
container.removeFromSuperview()
}
/*
Define UIColor from hex value
@param rgbValue - hex color value
@param alpha - transparency level
*/
func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor {
let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
let blue = CGFloat(rgbValue & 0xFF)/256.0
return UIColor(red:red, green:green, blue:blue, alpha:CGFloat(alpha))
}
}
谢谢。
答案 0 :(得分:1)
在您的代码中,您将创建2个不同的ViewControllerUtils
对象。您正在显示第一个,而隐藏第二个。
您可以按以下步骤解决它:
let indicator = ViewControllerUtils()
indicator.showActivityIndicator(uiView: self.view)
indicator.hideActivityIndicator(uiView: self.view)