簡體   English   中英

在uiwebview上添加活動指示器

[英]Add an activity indicator on a uiwebview

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

加載視圖:

@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>框架和corse,將QuartzCore框架添加到你的項目中!

檢測webview停止加載時:

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

你必須實現UIWebViewDelegate協議,和

webView.delegate = self;

並使其可見:

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

 [loadingView setHidden:NO];

}

如何僅在網頁出現時停止活動指示器?

您的對象可以成為webView delegate ,以偵聽-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

在此輸入圖像描述

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

標題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM