Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add UIActivityIndicatorView into UIBarButton

How do I add a UIActivityIndicatorView spinner circle into a UIBarButton, so that when a user taps on one of those buttons on the navigation bar, they see a spinner while the loading takes place?

like image 725
quantum Avatar asked Nov 12 '09 02:11

quantum


3 Answers

If you're trying to show the activity wheel in a navigation bar button (e.g. you might have a refresh button on your navbar) - you can create a new UIBarButtonItem with a custom view being the UIActivityIndicatorView:

Objective-C

uiBusy = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
uiBusy.hidesWhenStopped = YES;
[uiBusy startAnimating];
[self.navigationItem.rightBarButtonItem initWithCustomView:uiBusy];

Swift

let uiBusy = UIActivityIndicatorView(activityIndicatorStyle: .White)
uiBusy.hidesWhenStopped = true
uiBusy.startAnimating()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: uiBusy)

This overwrites your rightBarButtonItem with the spinning wheel. When you're done, just recreate the rightBarButtonItem.

like image 75
ferdil Avatar answered Nov 13 '22 08:11

ferdil


activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.hidesWhenStopped = YES;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];

Place the following where ever is needed:

[activityIndicator startAnimating];
[activityIndicator stopAnimating];
like image 39
emotality Avatar answered Nov 13 '22 07:11

emotality


Actually activity indicator is not added as toolbar item. It's a subview of current view.


    UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [act setCenter:CGPointMake(20, 20)];
    [act startAnimating];
    [self.view addSubview:act];

Remember to release it in -(void)dealloc.

like image 2
Chilly Zhong Avatar answered Nov 13 '22 07:11

Chilly Zhong