Created
October 27, 2019 16:38
-
-
Save JesusMartinAlonso/5661ec04485105717929ec6fd89ac7c8 to your computer and use it in GitHub Desktop.
BaseViewController in swift to allows show loading in any viewcontroller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BaseViewController.swift | |
// iOS architecture | |
// | |
// Created by Jesus Martin Alonso on 27/10/2019. | |
// | |
import UIKit | |
class BaseViewController: UIViewController { | |
var loadingView: UIView! | |
var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
configureLoadingView() | |
} | |
fileprivate func configureLoadingView() { | |
//Adds loading view | |
loadingView = UIView(frame: view.bounds) | |
loadingView.autoresizingMask = [.flexibleWidth, .flexibleHeight] | |
loadingView.backgroundColor = LoadingViewStyle.backgroundColor | |
loadingView.isHidden = true | |
loadingView.isAccessibilityElement = true | |
loadingView.accessibilityLabel = "LoadingView" | |
loadingIndicator.isAccessibilityElement = true | |
loadingIndicator.accessibilityLabel = "LoadingIndicator" | |
loadingIndicator.style = .whiteLarge | |
loadingIndicator.hidesWhenStopped = true | |
loadingIndicator.color = BaseViewControllerStyle.loadingIndicatorColor | |
self.view.addSubview(loadingView) | |
self.view.addSubview(loadingIndicator) | |
loadingIndicator.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
loadingIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor), | |
loadingIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor) | |
]) | |
view.bringSubviewToFront(loadingIndicator) | |
} | |
func showLoading(loading: Bool) { | |
if loading { | |
loadingIndicator.startAnimating() | |
loadingView.isHidden = false | |
view.bringSubviewToFront(loadingView) | |
view.bringSubviewToFront(loadingIndicator) | |
} else { | |
self.loadingIndicator.stopAnimating() | |
self.loadingView.isHidden = true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment