iphone-sdk-3.1uiactivityindicatorviewuibuttonbaritem

UIActivityIndicatorView inside UIButtonBarItem not working properly


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?


Solution

  • 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];