ios 在 uiwebview 上添加活动指示器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19475350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:26:49  来源:igfitidea点击:

Add an activity indicator on a uiwebview

iosuiwebviewios7uiactivityindicatorview

提问by cdub

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"?

我有一个 viewDidLoad,它创建一个 web 视图并启动一个活动指示器。如何使活动指示器仅在网页出现时停止?如何在活动指示器中添加诸如“加载中”之类的词?

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

回答by incmiko

Make a loading View:

制作一个加载视图:

@implementation yourViewController{;
    UIView* loadingView;
}

In your viewDidLoad:

在您的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 QuartzCoreframework to your project !

小心,如果你想使用cornerRadius,你必须导入<QuartzCore/QuartzCore.h>framework,并且在导入之前的corse,将QuartzCoreframework添加到你的项目中!

Detect when webview stop loading:

检测 webview 何时停止加载:

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

you have to implement UIWebViewDelegateprotocol, and

你必须实现UIWebViewDelegate协议,并且

webView.delegate = self;

and make it visible:

并使其可见:

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

 [loadingView setHidden:NO];

}

回答by LorikMalorik

How do I make the activity indicator stop only when the web page appears?

如何使活动指示器仅在网页出现时停止?

Your object can become delegatefor webViewto listen to -webViewDidFinishLoaddelegate method. So:

你的目标可以成为delegatewebView-webViewDidFinishLoad委托方法。所以:

- (void)viewDidLoad {
    // Create the UIWebView
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    webView.delegate = self; // Here is the key
    ...
}

...

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.view viewWithTag:100].hidden = YES;
}

You may also implement -webViewDidStartLoad:to show activity indicator instead of showing it in -viewDidLoadmethod.

您还可以实现-webViewDidStartLoad:显示活动指示器而不是在-viewDidLoad方法中显示它。

How do I add words to the activity indicator like "Loading"?

如何在活动指示器中添加诸如“加载中”之类的词?

You should create a separate UILabel, there is no way to add text to standard UIActivityIndicatorView

您应该创建一个单独的UILabel,无法将文本添加到标准UIActivityIndicatorView

回答by tomDev

enter image description here

在此处输入图片说明

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

如果你正在 Swift 上寻找这个,这里是:

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()
    boxView.alpha = 0.9
    boxView.layer.cornerRadius = 10

    // Spin config:
    let activityView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    activityView.frame = CGRect(x: 20, y: 12, width: 40, height: 40)
    activityView.startAnimating()

    // Text config:
    let textLabel = UILabel(frame: CGRect(x: 0, y: 50, width: 80, height: 30))
    textLabel.textColor = UIColor.whiteColor()
    textLabel.textAlignment = .Center
    textLabel.font = UIFont(name: textLabel.font.fontName, size: 13)
    textLabel.text = "Loading..."

    // Activate:
    boxView.addSubview(activityView)
    boxView.addSubview(textLabel)
    view.addSubview(boxView)
}

func webViewDidFinishLoad(webView_Pages: UIWebView) {

    // Removes it:
    boxView.removeFromSuperview()

}

Don't forget to call UIWebViewDelegate:

不要忘记调用 UIWebViewDelegate:

class ViewController: UIViewController, UIWebViewDelegate {

And set the delegate for the webView on the viewDidLoad:

并在 viewDidLoad 上设置 webView 的委托:

webView.delegate = self

回答by SWAMY CHUNCHU

Header:

标题:

    UIView * indicatorView;
    UIActivityIndicatorView *activityIndicator;

ViewDidLoad:

ViewDidLoad:

@property UILabel* label;


-(void)viewDidLoad {
[super viewDidLoad];


indicatorView=[[UIView alloc]initWithFrame:CGRectMake(self.mapView.frame.size.width/2-40, self.mapView.frame.size.height/2-30 , 80, 60)];
indicatorView.backgroundColor=[[UIColor blackColor]colorWithAlphaComponent:0.7f];
[self.mapView addSubview:indicatorView];
indicatorView.layer.cornerRadius=5;
[[indicatorView layer] setBorderWidth:1.0f];
[[indicatorView layer] setBorderColor:[UIColor whiteColor].CGColor];
indicatorView.clipsToBounds=YES;


activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(indicatorView.frame.size.width/2-15, 8, 30, 30)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
//activityIndicator.backgroundColor=[UIColor greenColor];
[indicatorView addSubview:activityIndicator];
[activityIndicator startAnimating];


UILabel *loadingLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, indicatorView.frame.size.height-22, indicatorView.frame.size.width, 20)];
loadingLbl.text = @"  Loading...";
//loadingLbl.backgroundColor=[UIColor redColor];
loadingLbl.textColor = [UIColor whiteColor];
loadingLbl.textAlignment = NSTextAlignmentCenter;
loadingLbl.font = [UIFont fontWithName:@"YanaR-Bold" size:10];
[loadingLbl  setFont:[UIFont boldSystemFontOfSize:10.0f]];
[indicatorView addSubview:loadingLbl];