Skip to content

Instantly share code, notes, and snippets.

@igordeoliveirasa
Created March 26, 2015 01:41
Show Gist options
  • Save igordeoliveirasa/78a310f0348fcad9b270 to your computer and use it in GitHub Desktop.
Save igordeoliveirasa/78a310f0348fcad9b270 to your computer and use it in GitHub Desktop.
iOS Loading Overlay View - SWIFT
//
// LoadingOverlay.swift
// app
//
// Created by Igor de Oliveira Sa on 25/03/15.
// Copyright (c) 2015 Igor de Oliveira Sa. All rights reserved.
//
// Usage:
//
// # Show Overlay
// LoadingOverlay.shared.showOverlay(self.navigationController?.view)
//
// # Hide Overlay
// LoadingOverlay.shared.hideOverlayView()
import UIKit
import Foundation
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 = UIView(frame: UIScreen.mainScreen().bounds)
overlayView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
activityIndicator.center = overlayView.center
overlayView.addSubview(activityIndicator)
activityIndicator.startAnimating()
view.addSubview(overlayView)
}
public func hideOverlayView() {
activityIndicator.stopAnimating()
overlayView.removeFromSuperview()
}
}
@geekforbrains
Copy link

Beautifully written and exactly what I needed. Thanks for saving me some time :)

@prwiley
Copy link

prwiley commented Nov 6, 2015

This is generating an error for me "unexpectedly found nil . . ." at line 39

@ctouchstone
Copy link

I'm getting an error on line#39 as well.
Error: "Unexpectedly found nil while unwrapping an Optional value."

@ctouchstone
Copy link

Changing:
"LoadingOverlay.shared.showOverlay(self.navigationController?.view)"
to
"LoadingOverlay.shared.showOverlay(self.view)"

Seems worked for me.

@aw230012
Copy link

aw230012 commented Dec 7, 2015

Nice solution. I would suggest making your vars private so they're hidden from invoking objects. Only allow access to your functions. I also added a class level boolean called visible that will track whether the overlay is visible or not. This way a call into stopAnimating and removeFromSuperview only occur if the overlay is visible.

@aw230012
Copy link

aw230012 commented Dec 7, 2015

@prwiley and @ctouchstone, you probably do not have a navigation controller initialized. You can get one easily by adding it to the storyboard or you can create a main navigation controller, see http://stackoverflow.com/questions/26753925/set-initial-viewcontroller-in-appdelegate-swift

@Hecel85
Copy link

Hecel85 commented Jul 13, 2016

Exactly what I needed. Thanks for saving me some time 👍

@ktennie
Copy link

ktennie commented Sep 13, 2016

Works great :-) Thanks

@heijmerikx
Copy link

heijmerikx commented Jan 28, 2017

Thanks a lot 👍

@anoop4real
Copy link

What about user interactions?

@dushmis
Copy link

dushmis commented Jan 2, 2018

This won't work for view gestures

@egorovmikhail
Copy link

if there is UIKit, then Foundation is unnecessary to import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment