I would like to change the content of a UIButton to an ActivityIndicator after it is pressed.
I know buttons have an imageView and a titleLabel, but I don\'t know how to
Here is my edit with little less code.
class Button: UIButton {
private var originalButtonText: String?
private lazy var activityIndicator: UIActivityIndicatorView = {
let activityIndicator = UIActivityIndicatorView()
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
activityIndicator.color = .black
addSubview(activityIndicator)
NSLayoutConstraint.activate([
activityIndicator.centerYAnchor.constraint(equalTo: self.centerYAnchor),
activityIndicator.centerXAnchor.constraint(equalTo: self.centerXAnchor)
])
return activityIndicator
}()
func loading(_ isLoading: Bool) {
isEnabled = !isLoading
if isLoading {
originalButtonText = titleLabel?.text
setTitle("", for: .normal)
activityIndicator.startAnimating()
} else {
setTitle(originalButtonText, for: .normal)
activityIndicator.stopAnimating()
}
}
}
Usage: button.loading(true/false)