For me it took sometime to figure out how to show activity indicator in the navigation bar. Later I found it was very simple. When you push a view to the navigation you get to play with navigationItem property on the view that is being pushed. In your viewWillAppear method you can add this code.
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:YES]; UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; [activityIndicator stopAnimating]; [activityIndicator hidesWhenStopped]; UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator]; [self navigationItem].rightBarButtonItem = barButton; [activityIndicator release]; [barButton release]; }
I have done a sample application demonstrating this code. It is based on iPhone navigation project template. In the application I have two action to start and stop the animation.
-(IBAction)startActivity:(id)sender{ [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView startAnimating]; } -(IBAction)stopActivity:(id)sender{ [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView stopAnimating]; }
Screen shot of the application
Download Load Activity Indicator Xcode project files.
this is great, just what i was looking for but where do i insert the code?
How would I START the animation before I “visit the web to get some data” and then STOP the animation after the data is received?
“Demo code is great”… but “real world use examples” are even better.
(There aren’t too many people that just need 2 buttons to manually start/stop the animation.)
Thanks for the short tut