Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an "overlay" when running long tasks in iOS

What is example for loading overlay in Swift IOS application when do a long tasks. Example for loading data from remote server. I googled but not found any answer.

Updated:

Thanks for @Sebastian Dressler this is simple way. I updated my code and it run cool

public class LoadingOverlay{  var overlayView = UIView() var activityIndicator = UIActivityIndicatorView()  class var shared: LoadingOverlay {     struct Static {         static let instance: LoadingOverlay = LoadingOverlay()     }     return Static.instance }      public func showOverlay(view: UIView) {          overlayView.frame = CGRectMake(0, 0, 80, 80)         overlayView.center = view.center         overlayView.backgroundColor = UIColor(hex: 0x444444, alpha: 0.7)         overlayView.clipsToBounds = true         overlayView.layer.cornerRadius = 10          activityIndicator.frame = CGRectMake(0, 0, 40, 40)         activityIndicator.activityIndicatorViewStyle = .WhiteLarge         activityIndicator.center = CGPointMake(overlayView.bounds.width / 2, overlayView.bounds.height / 2)          overlayView.addSubview(activityIndicator)         view.addSubview(overlayView)          activityIndicator.startAnimating()     }      public func hideOverlayView() {         activityIndicator.stopAnimating()         overlayView.removeFromSuperview()     } } 

let using:

LoadingOverlay.shared.showOverlay(self.view) //To to long tasks LoadingOverlay.shared.hideOverlayView() 
like image 378
Sonrobby Avatar asked Jan 15 '15 09:01

Sonrobby


Video Answer


2 Answers

The above answers add a loading view but it doesn't block click events on the screen also it does not provides overlay for rest of screen. You can achieve it as follows:

let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .Alert)  alert.view.tintColor = UIColor.blackColor() let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(10, 5, 50, 50)) as UIActivityIndicatorView loadingIndicator.hidesWhenStopped = true loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray loadingIndicator.startAnimating();  alert.view.addSubview(loadingIndicator) presentViewController(alert, animated: true, completion: nil) 

Swift 3.0

let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)  let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50)) loadingIndicator.hidesWhenStopped = true loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray loadingIndicator.startAnimating();  alert.view.addSubview(loadingIndicator) present(alert, animated: true, completion: nil) 

Swift 4.0 and newer

let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)  let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50)) loadingIndicator.hidesWhenStopped = true loadingIndicator.style = UIActivityIndicatorView.Style.gray loadingIndicator.startAnimating();  alert.view.addSubview(loadingIndicator) present(alert, animated: true, completion: nil) 

and you can hide it as follows:

dismiss(animated: false, completion: nil) 

It will be shown as follows: enter image description here

like image 145
Ajinkya Patil Avatar answered Oct 04 '22 16:10

Ajinkya Patil


Just create yourself an overlay view, which you add to your parent view and remove it once your task is done, e.g. to add it:

var overlay : UIView? // This should be a class variable  [ ... ]  overlay = UIView(frame: view.frame) overlay!.backgroundColor = UIColor.blackColor() overlay!.alpha = 0.8  view.addSubview(overlay!) 

For removal:

overlay?.removeFromSuperview() 
like image 32
Sebastian Dressler Avatar answered Oct 04 '22 16:10

Sebastian Dressler