Skip to content

Instantly share code, notes, and snippets.

@sukov
Last active June 22, 2021 15:10
Show Gist options
  • Save sukov/b4949987bfe96754575265c740aea32f to your computer and use it in GitHub Desktop.
Save sukov/b4949987bfe96754575265c740aea32f to your computer and use it in GitHub Desktop.

Revisions

  1. sukov revised this gist Jun 22, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion UIButtonExtension.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    extension UIButton {
    /// Unique number used to tag the loading indicator view
    private var loadingIndicatorTag: Int {
    808404
    @@ -26,4 +27,5 @@
    alpha = 1.0
    loadingIndicator?.stopAnimating()
    loadingIndicator?.removeFromSuperview()
    }
    }
    }
  2. sukov revised this gist Jun 22, 2021. 1 changed file with 28 additions and 23 deletions.
    51 changes: 28 additions & 23 deletions UIButtonExtension.swift
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,29 @@
    extension UIButton {
    func loadingIndicator(_ show: Bool) {
    let tag = 808404
    if show {
    self.isEnabled = false
    self.alpha = 0.8
    let indicator = UIActivityIndicatorView()
    let buttonHeight = self.bounds.size.height
    let buttonWidth = self.bounds.size.width
    let titleLabelMaxX = titleLabel?.frame.maxX ?? 0
    indicator.center = CGPoint(x: titleLabelMaxX + (buttonWidth - titleLabelMaxX) / 2, y: buttonHeight / 2)
    indicator.tag = tag
    self.addSubview(indicator)
    indicator.startAnimating()
    } else {
    self.isEnabled = true
    self.alpha = 1.0
    if let indicator = self.viewWithTag(tag) as? UIActivityIndicatorView {
    indicator.stopAnimating()
    indicator.removeFromSuperview()
    }
    }
    /// Unique number used to tag the loading indicator view
    private var loadingIndicatorTag: Int {
    808404
    }
    }

    private var loadingIndicator: UIActivityIndicatorView? {
    viewWithTag(loadingIndicatorTag) as? UIActivityIndicatorView
    }

    func displayLoadingIndicator() {
    isEnabled = false
    alpha = 0.8
    let indicator = UIActivityIndicatorView(style: .medium)
    indicator.color = .white
    let buttonHeight = self.bounds.size.height
    let buttonWidth = self.bounds.size.width
    let titleLabelMaxX = titleLabel?.frame.maxX ?? 0
    indicator.center = CGPoint(x: titleLabelMaxX + (buttonWidth - titleLabelMaxX) / 2, y: buttonHeight / 2)
    indicator.tag = loadingIndicatorTag
    addSubview(indicator)
    indicator.startAnimating()
    }

    func hideLoadingIndicator() {
    isEnabled = true
    alpha = 1.0
    loadingIndicator?.stopAnimating()
    loadingIndicator?.removeFromSuperview()
    }
  3. sukov created this gist Jun 30, 2019.
    24 changes: 24 additions & 0 deletions UIButtonExtension.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    extension UIButton {
    func loadingIndicator(_ show: Bool) {
    let tag = 808404
    if show {
    self.isEnabled = false
    self.alpha = 0.8
    let indicator = UIActivityIndicatorView()
    let buttonHeight = self.bounds.size.height
    let buttonWidth = self.bounds.size.width
    let titleLabelMaxX = titleLabel?.frame.maxX ?? 0
    indicator.center = CGPoint(x: titleLabelMaxX + (buttonWidth - titleLabelMaxX) / 2, y: buttonHeight / 2)
    indicator.tag = tag
    self.addSubview(indicator)
    indicator.startAnimating()
    } else {
    self.isEnabled = true
    self.alpha = 1.0
    if let indicator = self.viewWithTag(tag) as? UIActivityIndicatorView {
    indicator.stopAnimating()
    indicator.removeFromSuperview()
    }
    }
    }
    }