1
votes

I'm using activity indicator view in my app.

When I click on a button, I want the activity indicator to display in the navigation bar for 10 seconds then automatically hide.

I'm using the following code in view did load:

CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);

self.activity = [[UIActivityIndicatorView alloc]
             initWithFrame:frame];

[self.activity sizeToFit];

self.activity.autoresizingMask =
    (UIViewAutoresizingFlexibleLeftMargin |
     UIViewAutoresizingFlexibleRightMargin |
     UIViewAutoresizingFlexibleTopMargin |
     UIViewAutoresizingFlexibleBottomMargin);

UIBarButtonItem *loadingView = [[UIBarButtonItem alloc] initWithCustomView:self.activity];
loadingView.target = self;
self.navigationItem.rightBarButtonItem = loadingView;   

And in the button action I only start the indicator:

[self.activity startanimating];

But I cannot see the indicator in navigation bar.

Please tell me if there are any problems in my code...

2

2 Answers

5
votes
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
UIBarButtonItem *itemIndicator = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[self.navigationItem setLeftBarButtonItem:itemIndicator];
[activityIndicator startAnimating];
0
votes

I don't understand why you're using target (and you even do not specify an action)...anyway I just tested your code and introduced this slight adjustment. Every time I want to start the activity indicator I use this piece of code:

UIActivityIndicatorView *test = (UIActivityIndicatorView*)self.navigationItem.rightBarButtonItem.customView;

[test startAnimating];

Obviously I don't use an ivar because it's not needed. It's just a cast and from your UIViewController and you can obtain it in a very fast way.

Hope it helps.