在uiwebview上添加活动指示器

时间:2013-10-20 08:25:24

标签: ios uiwebview ios7 uiactivityindicatorview

我有一个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];

4 个答案:

答案 0 :(得分:34)

制作加载视图:

@implementation yourViewController{;
    UIView* loadingView;
}

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

此视图如下所示:

enter image description here

注意,如果你想使用cornerRadius,你必须在导入之前导入<QuartzCore/QuartzCore.h>框架和corse,将QuartzCore框架添加到你的项目中!

检测webview何时停止加载:

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

您必须实施UIWebViewDelegate协议和

webView.delegate = self;

并使其可见:

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

 [loadingView setHidden:NO];

}

答案 1 :(得分:32)

  

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

delegate webView您的对象可以-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; } 委托方法。所以:

-webViewDidStartLoad:

您也可以实施-viewDidLoad来显示活动指标,而不是在UILabel方法中显示。

  

如何在活动指标中添加单词,如“正在加载”?

您应该创建单独的UIActivityIndicatorView,无法将文字添加到标准{{1}}

答案 2 :(得分:9)

enter image description here

如果你在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()

}

不要忘记调用UIWebViewDelegate:

class ViewController: UIViewController, UIWebViewDelegate {

并在viewDidLoad上设置webView的委托:

webView.delegate = self

答案 3 :(得分:1)

部首:

    UIView * indicatorView;
    UIActivityIndicatorView *activityIndicator;

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