Thursday, July 12, 2012

iOS Activity Indicator

This is how it is done (example is using WebView)
Link: http://stackoverflow.com/questions/11334247/add-activity-indicator-to-web-view

I made some further changes so the indicator is more visible (by putting it inside an alert popup):

- (void)viewDidLoad
{
    [super viewDidLoad];    
    loadingAlert = [[UIAlertView alloc] initWithTitle:@"Loading..."
                                                      message:@"\n"
                                                     delegate:self
                                            cancelButtonTitle:nil
                                            otherButtonTitles:nil, nil];

    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [loadingAlert addSubview:activityIndicator];
}

- (void) showLoadingAlert
{
    [loadingAlert show];    
    activityIndicator.frame = CGRectMake((x,y,w,h);
    [activityIndicator startAnimating];
}
- (void) dismissLoadingAlert
{
    [activityIndicator stopAnimating];
    [loadingAlert dismissWithClickedButtonIndex:0 animated:NO];
}

Note that I set the frame after "show". This is because if I wanted to calculate x,y,w,h relative to the size of the loading alert frame, these values will only be non-zero after show.

No comments:

Post a Comment