Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a UIActivityView icon on UIToolBar?

How do I add an activity indicator to my toolbar, like the Mail app does when it is checking for email?

like image 945
WangYang Avatar asked Feb 25 '11 05:02

WangYang


1 Answers

If you want to add it through code, not though interface builder, you need to:

  1. Create the activity indicator
  2. Create UIBarButtonItem that will show the activity indicator
  3. Add it into an array of views which will go into your toolbar
  4. Put that array in your toolbar

Here's a code sample:

- (void) showActivityIndicator{

    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [activityView startAnimating];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:activityView];
    NSArray *items = [[NSArray alloc] initWithObjects:item, nil];
    [self.navigationController.toolbar setItems:items];
    [items release];
    [activityView release]; 
}
like image 120
Moshe Avatar answered Oct 22 '22 08:10

Moshe