I am using http communication in My iPhone app. I want to show a progress bar while it is loading data from server. How can I do it programmatically?
I just want a d
Swift 5.x Version
import UIKit
class SKLoader: NSObject {
static let sharedInstance = SKLoader()
let indicator: UIActivityIndicatorView? = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.medium)
let screen = UIScreen.main.bounds
var appDelegate: SceneDelegate {
guard let sceneDelegate = UIApplication.shared.connectedScenes
.first!.delegate as? SceneDelegate else {
fatalError("sceneDelegate is not UIApplication.shared.delegate")
}
return sceneDelegate
}
var rootController:UIViewController? {
guard let viewController = appDelegate.window?.rootViewController else {
fatalError("There is no root controller")
}
return viewController
}
func show() {
indicator?.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
indicator?.frame.origin.x = (screen.width/2 - 20)
indicator?.frame.origin.y = (screen.height/2 - 20)
rootController?.view.addSubview(indicator!)
indicator?.startAnimating()
}
func hide() {
DispatchQueue.main.async {
self.indicator?.stopAnimating()
self.indicator?.removeFromSuperview()
}
}
}
Sample Usage:
//To Show
SKLoader.sharedInstance.show()
//To Hide
SKLoader.sharedInstance.hide()
=======