Adding an activity indicator to the navigation bar

I have a view controller that starts an asynchronous network request when the user clicks a button, and then pops another controller after the request is complete. At this intermediate time, I want the activity indicator to be displayed on the navigation bar, so the user knows that he is retrieving data. I have this code in IBAction for a button:

self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite]; // I've tried Gray, White, and WhiteLarge [self.activityIndicator startAnimating]; self.activityIndicator.hidden = NO; UIBarButtonItem* spinner = [[UIBarButtonItem alloc] initWithCustomView: self.activityIndicator]; self.navigationItem.rightBarButtonItem = spinner; 

The indicator does not appear, but this code is called. I looked at other posts on this topic, and they say, to do just that. The user interface thread is not displayed during a call (I can press other buttons and go to another path while waiting). Did I miss something?

+6
source share
3 answers

I solved it.

There was a cancel button that went off when the keyboard did. The code that deleted the cancel button did not care if it deleted the button or my counter. I moved the spinner code to a later location, and all is well.

+2
source

I'm having trouble displaying a UIBarButtonItem if it already exists in this place. If so, try:

 self.navigationItem.rightBarButtonItem = nil; self.navigationItem.rightBarButtonItem = spinner; 
0
source

You can also try to set the panel button element this way (if you are targeting at least iOS 5):

 self.navigationItem.rightBarButtonItems = @[spinner]; 
0
source

All Articles