I am trying to show a UIActivityIndicatorView at the same location of the UIBarButtonItem it replaces. It shifts to the right while animating. How can I center it without changing the style?
Here is the code:
@IBAction func upload(_ sender: Any) {
let item = sender as! UIBarButtonItem
let indicatorView = UIActivityIndicatorView(style: .medium)
indicatorView.translatesAutoresizingMaskIntoConstraints = false
item.customView = indicatorView
indicatorView.startAnimating()
DispatchQueue.global().async {
Thread.sleep(forTimeInterval: 5)
DispatchQueue.main.async {
item.customView = nil
}
}
}
I would create a custom
UIViewthat contains both the text/icon for the bar button item and the activity indicator, all with constraints set and when I need it do load/animate or not I'd do it directly on that custom view and hide/show the elements accordingly. TheUIBarButtonItemis initialized with that custom view.