Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an activity indicator on a uiwebview

I have a viewDidLoad which creates a web view and starts an activity indicator. How do I make the activity indicator stop only when the web page appears? How do I add words to the activity indicator like "Loading"?

// Create the UIWebView
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];

[self.view addSubview:webView];

// Start the throbber to check if the user exists
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center = CGPointMake([self screenWidth] / 2.0, 370.0);
[activityView startAnimating];
activityView.tag = 100;
[self.view addSubview:activityView];

NSString* url = @"http://google.com";
NSURL* nsUrl = [NSURL URLWithString:url];
NSURLRequest* request = [NSURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];


// Remove activity view
UIView *viewToRemove = [self.view viewWithTag:100];
//[viewToRemove removeFromSuperview];

[webView loadRequest:request];
like image 621
cdub Avatar asked Oct 20 '13 08:10

cdub


People also ask

How do I display activity indicator in Swift?

Select the Assistant Editor and make sure the ViewController. swift is visible. Ctrl and drag from the Start Button to the ViewController class and create the following Action. Ctrl and drag from the Stop Button to the ViewController class and create the following Action.

How do I get rid of the activity indicator?

Select the Activity Indicator in Storyboard, then select property "Hides when stopped". This way you don't have to hide it, just start or stop the animation, and the Activity Indicator will show and hide automatically. Of course, you still have to add the code to start and stop the animation to buttons.


Video Answer


1 Answers

Make a loading View:

@implementation yourViewController{;
    UIView* loadingView;
}

In your viewDidLoad:

loadingView = [[UIView alloc]initWithFrame:CGRectMake(100, 400, 80, 80)];
loadingView.backgroundColor = [UIColor colorWithWhite:0. alpha:0.6];
loadingView.layer.cornerRadius = 5;

UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = CGPointMake(loadingView.frame.size.width / 2.0, 35);
[activityView startAnimating];
activityView.tag = 100;
[loadingView addSubview:activityView];

UILabel* lblLoading = [[UILabel alloc]initWithFrame:CGRectMake(0, 48, 80, 30)];
lblLoading.text = @"Loading...";
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont fontWithName:lblLoading.font.fontName size:15];
lblLoading.textAlignment = NSTextAlignmentCenter;
[loadingView addSubview:lblLoading];

[self.view addSubview:loadingView];

This view will look like this:

enter image description here

Be careful, if you want to use the cornerRadius, you have to import <QuartzCore/QuartzCore.h> framework, and of corse before import, add QuartzCore framework to your project !

Detect when webview stop loading:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [loadingView setHidden:YES];
}

you have to implement UIWebViewDelegate protocol, and

webView.delegate = self;

and make it visible:

- (void)webViewDidStartLoad:(UIWebView *)webView {

 [loadingView setHidden:NO];

}
like image 100
incmiko Avatar answered Nov 02 '22 23:11

incmiko