Here is the best solution for adding loader while loading webview. Many developers need the UI like when user interact with app and webview is there in app. So when webview is loading user can see loader and wait for the response.

For Objective C:

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:

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

For Swift:

If you are looking for this on Swift, here it is:

var boxView = UIView()func webViewDidStartLoad(webView_Pages: UIWebView) {    // Box config:
boxView = UIView(frame: CGRect(x: 115, y: 110, width: 80, height: 80))
boxView.backgroundColor = UIColor.blackColor()…

--

--

Mr.Javed Multani

Software Engineer | Certified ScrumMaster® (CSM) | UX Researcher | Youtuber | Tech Writer