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
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
indicator.center = self.view.center;
[self.view addSubview:indicator];
[indicator bringSubviewToFront:self.view];
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;
Write below code when you want to show indicator
[indicator startAnimating];
write below code when you want to hide indicator
[indicator stopAnimating];
Translating @Hiren's answer to Swift
var indicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
indicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
indicator.center = view.center
self.view.addSubview(indicator)
self.view.bringSubview(toFront: indicator)
UIApplication.shared.isNetworkActivityIndicatorVisible = true
Show indicator
indicator.startAnimating()
Stop indicator
indicator.stopAnimating()
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()
=======
I know this is an older question but I've written a global static method for doing this type of thing from any view, based on the above answers.
Main updates are:
In my AppHelper.swift:
static func showActivityIndicator(view: UIView, withOpaqueOverlay: Bool) {
// this will be the alignment view for the activity indicator
var superView: UIView = view
// if we want an opaque overlay, do that work first then put the activity indicator within that view; else just use the passed UIView to center it
if withOpaqueOverlay {
let overlay = UIView()
overlay.frame = CGRectMake(0.0, 0.0, view.frame.width, view.frame.height)
overlay.layer.backgroundColor = UIColor.blackColor().CGColor
overlay.alpha = 0.7
overlay.tag = activityIndicatorOverlayViewTag
overlay.center = superView.center
overlay.hidden = false
superView.addSubview(overlay)
superView.bringSubviewToFront(overlay)
// now we'll work on adding the indicator to the overlay (now superView)
superView = overlay
}
let indicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
indicator.center = superView.center
indicator.tag = activityIndicatorViewTag
indicator.hidden = false
superView.addSubview(indicator)
superView.bringSubviewToFront(indicator)
indicator.startAnimating()
// also indicate network activity in the status bar
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
static func hideActivityIndicator(view: UIView) {
// stop the network activity animation in the status bar
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
// remove the activity indicator and optional overlay views
view.viewWithTag(activityIndicatorViewTag)?.removeFromSuperview()
view.viewWithTag(activityIndicatorOverlayViewTag)?.removeFromSuperview()
}
Add Property
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *indicator;
To Start Animating
[self.indicator startAnimating];
To Stop Animating
[self.indicator stopAnimating];
Try MBProgressHUD.
It's pretty simple, got several progress animation options, and able to add some customization. It displays fullscreen. And it should work with any recent iOS versions.
Or try LDProgressView if you want something more fancy :) It somehow looks like OSX progress bar.