【发布时间】:2018-08-26 03:44:21
【问题描述】:
我正在为我的 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))
}
}
提前致谢。
【问题讨论】:
-
您将
UIActivityIndicatorView对象存储在哪里?也显示ViewControllerUtils的代码。\ -
我可以看到
show和hide正文 -
我将
UIActivityIndicatorView存储在一个单独的文件中。我用ViewControllerUtils代码更新了我的问题。