Is it possible to display an UIActivityIndicator in the UISearchBar while searching?
Found none of the advertised solutions worked well for iOS13... here is my own, which subclasses the Search Bar and should be more robust for future use
class SearchBar: UISearchBar {
var activityIndicator: UIActivityIndicatorView?
var isLoading: Bool {
get {
return activityIndicator != nil
} set {
if newValue {
if activityIndicator == nil {
guard let leftView = searchTextField.leftView else {
return
}
let ai = UIActivityIndicatorView(style: .medium)
ai.frame = self.convert(leftView.frame, from: leftView.superview)
self.addSubview(ai)
ai.startAnimating()
leftView.isHidden = true
activityIndicator = ai
}
} else {
activityIndicator?.removeFromSuperview()
activityIndicator = nil
guard let leftView = searchTextField.leftView else {
return
}
leftView.isHidden = false
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With