Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an activity indicator to the nav bar

I have a view controller that starts an async network request when the user taps a button, then pushes another controller after the request finishes. In that intervening time, I want the navigation bar to display an activity indicator, so the user knows it's fetching data. I have this code in the IBAction for the 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 getting called. I've looked at other posts on the topic, and they say to do exactly this. The UI thread does not appear blocked during the call (I can tap other buttons and navigate a different path while waiting). Am I missing something?

like image 550
kwiqsilver Avatar asked Feb 17 '14 21:02

kwiqsilver


People also ask

How do I add a navigation bar in Swift storyboard?

Go to the Storyboard. Select the View Controller and in The Editor menu select Embed in -> Navigation Controller. Next, drag a Bar Button from the Object Library to the left side of the Navigation Bar and name it "Left Item". Repeat this for the right side and name it "Right Item".


1 Answers

I solved it.

There was a cancel button there already that was going away when the keyboard did. The code that removed the cancel button didn't care whether it was removing a button or my spinner. I moved the spinner code to a later location, and all is good.

like image 97
kwiqsilver Avatar answered Oct 15 '22 16:10

kwiqsilver