views:

305

answers:

2

I am trying to get a UIActivityIndicatorView to work inside of a UIButtonBarItem using the following code:

- (void)showActivityIndicator
{
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [activityIndicator startAnimating];

    UIBarButtonItem *activityBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
    [activityIndicator release];

    self.navigationItem.rightBarButtonItem = activityBarButtonItem;
    [activityBarButtonItem release];
}

It works to the point that the activity indicator is displayed, but it is not inside a button bar.

Does anyone have any ideas on what im doing wrong here?

A: 

but it is not inside a button bar

And where is it?

Maybe the frame you're setting is incorrect? Have you tried CGRectZero or something random (e.g. CGRectMake(40,60,20,20))? Does it have any impact on the position of the indicator?

Have you tried setting the width property of the item?

Adam Woś
A: 

This is what I did to get an activity indicator to appear when I wanted it to on the left-hand side of a toolbar:

activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0, 0.0, 25.0, 25.0)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
activityIndicator.hidesWhenStopped = YES;

UIBarButtonItem *btnActivity = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
NSMutableArray *tItems = [self.toolbar.items mutableCopy];
[tItems insertObject:btnActivity atIndex:0];
self.toolbar.items = tItems;
[tItems release];

Then, further on in my code when I want to show the activity indicator, I simply use it in the normal way:

[activityIndicator startAnimating];

And, when I don't want to see it anymore:

[activityIndicator stopAnimating];
Jeff Hay