16

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

4 回答 4

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

此视图将如下所示:

在此处输入图像描述

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

检测 webview 何时停止加载:

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

你必须实现UIWebViewDelegate协议,并且

webView.delegate = self;

并使其可见:

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

 [loadingView setHidden:NO];

}
于 2013-10-20T08:39:21.703 回答
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

于 2013-10-20T08:30:15.807 回答
9

在此处输入图像描述

如果你在 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
于 2016-01-15T18:11:45.797 回答
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];
于 2016-12-15T07:00:38.887 回答