LoadingViewController.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // LoadingViewController.swift
  3. // CMBMobile
  4. //
  5. // Created by LUO on 3/14/16.
  6. // Copyright © 2016 Yst-WHB. All rights reserved.
  7. //
  8. import UIKit
  9. class LoadingViewController: UIViewController {
  10. //use UIWindow show modal dialog
  11. var newWindow: UIWindow?
  12. func show() {
  13. if self.newWindow == nil {
  14. let uiwindow = UIWindow(frame: UIScreen.main.bounds)
  15. uiwindow.rootViewController = self
  16. uiwindow.isHidden = false
  17. uiwindow.backgroundColor = UIColor.clear
  18. self.newWindow = uiwindow
  19. }
  20. }
  21. func dismiss() {
  22. self.newWindow?.rootViewController = nil
  23. self.newWindow = nil
  24. }
  25. private var titleLabel: UILabel = UILabel()
  26. override func viewDidLoad() {
  27. let loadingView = UIView()
  28. let spining = UIActivityIndicatorView()
  29. view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)
  30. loadingView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)
  31. loadingView.layer.cornerRadius = 10
  32. spining.startAnimating()
  33. loadingView.translatesAutoresizingMaskIntoConstraints = false
  34. spining.translatesAutoresizingMaskIntoConstraints = false
  35. titleLabel.translatesAutoresizingMaskIntoConstraints = false
  36. titleLabel.textColor = UIColor.white
  37. titleLabel.font = UIFont.boldSystemFont(ofSize: 16)
  38. view.translatesAutoresizingMaskIntoConstraints = false
  39. let viewConstraints = [
  40. NSLayoutConstraint(item: loadingView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: -20),
  41. NSLayoutConstraint(item: loadingView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0)]
  42. view.addSubview(loadingView)
  43. view.addConstraints(viewConstraints)
  44. let loadingViewConstraints = [
  45. NSLayoutConstraint(item: spining, attribute: .centerX, relatedBy: .equal, toItem: loadingView, attribute: .centerX, multiplier: 1, constant: 0),
  46. NSLayoutConstraint(item: spining, attribute: .top, relatedBy: .equal, toItem: loadingView, attribute: .top, multiplier: 1, constant: 14),
  47. NSLayoutConstraint(item: titleLabel, attribute: .centerX, relatedBy: .equal, toItem: loadingView, attribute: .centerX, multiplier: 1, constant: 0),
  48. NSLayoutConstraint(item: titleLabel, attribute: .top, relatedBy: .equal, toItem: spining, attribute: .bottom, multiplier: 1, constant: 8),
  49. NSLayoutConstraint(item: titleLabel, attribute: .leading, relatedBy: .equal, toItem: loadingView, attribute: .leading, multiplier: 1, constant: 14),
  50. NSLayoutConstraint(item: loadingView, attribute: .bottom, relatedBy: .equal, toItem: titleLabel, attribute: .bottom, multiplier: 1, constant: 14)
  51. ]
  52. loadingView.addSubview(spining)
  53. loadingView.addSubview(titleLabel)
  54. loadingView.addConstraints(loadingViewConstraints)
  55. }
  56. func show(text: String) {
  57. self.show()
  58. self.titleLabel.text = text
  59. }
  60. }