在我的故事板中,我有一个UIWebView的视图,它填充整个区域,除了选项卡和导航栏.视图的控制器实现协议UIWebViewDelegate.控制器设置为UIWebView的委托:
#pragma mark - UIWebViewDelegate - (void) webViewDidStartLoad:(UIWebView *)webView { NSLog(@"webViewDidStartLoad"); } - (void) webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"webViewDidFinishLoad"); }
当UIWebView正在加载时,我将显示一个带有图像的黑色背景,表明它正在加载.我怎样才能做到这一点?
EDIT1:
我正在尝试添加默认值ActivityIndicatorView
而不是静态图像.但这只是给我一个黑色背景和一个小的白色区域.问题是什么?
UIImageView *loadingImageView = [[UIImageView alloc] initWithFrame:webView.frame]; UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; UIImage *spacer = [UIImage imageNamed:@"spacer"]; UIGraphicsBeginImageContext(spinner.frame.size); [spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)]; UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); loadingImageView.image = resizedSpacer; [loadingImageView setBackgroundColor:[UIColor blackColor]]; [loadingImageView setContentMode:UIViewContentModeCenter]; self.loadingImageView = loadingImageView;
Mutix.. 26
在你的控制器.h
@property (nonatomic, retain) UIImageView *loadingImageView;
在你的控制器.m
@synthesize loadingImageView; - (void)viewDidLoad { ... UIImageView *theLoadingImageView = [[UIImageView alloc] initWithFrame:webView.frame]; theLoadingImageView.image = [UIImage imageNamed:@"your_image.png"]; self.loadingImageView = theLoadingImageView; [theLoadingImageView release]; ... } - (void) webViewDidStartLoad:(UIWebView *)webView { [self.view addSubview:self.loadingImageView]; } - (void) webViewDidFinishLoad:(UIWebView *)webView { [self.loadingImageView removeFromSuperview]; }
编辑1:
如果你想显示一个ActivityIndicatorView而不是一个静态图像,这里有一个我喜欢使用的小帮助类:
LoadingIndicator.h
#import@interface LoadingIndicator : UIView { UIActivityIndicatorView *activityIndicator; UILabel *labelMessage; } @property(retain) UIActivityIndicatorView *activityIndicator; @property(retain) UILabel *labelMessage; - (id)init; - (void)show:(NSString *)theMessage; - (void)hide; - (void)refreshPosition; @end
LoadingIndicator.m
#import "LoadingIndicator.h" #import@implementation LoadingIndicator @synthesize labelMessage, activityIndicator; - (id)init { if (self = [super initWithFrame:CGRectMake(25, 130, 270, 100)]) { // Vue self.backgroundColor = [UIColor blackColor]; self.alpha = 0.80; self.layer.cornerRadius = 5; // Label : message UILabel *theLabelMessage = [[UILabel alloc] initWithFrame:CGRectMake(15, 65, 240, 20)]; theLabelMessage.backgroundColor = [UIColor clearColor]; theLabelMessage.textColor = [UIColor whiteColor]; theLabelMessage.text = NSLocalizedString(@"Loading", @"Loading"); theLabelMessage.textAlignment = UITextAlignmentCenter; theLabelMessage.font = [UIFont boldSystemFontOfSize:16]; theLabelMessage.adjustsFontSizeToFitWidth = YES; self.labelMessage = theLabelMessage; [self addSubview:theLabelMessage]; [theLabelMessage release]; // Activity Indicator UIActivityIndicatorView *theActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; theActivityIndicator.frame = CGRectMake(115, 15, 40, 40); self.activityIndicator = theActivityIndicator; [self addSubview:theActivityIndicator]; [theActivityIndicator release]; } return self; } - (void)show:(NSString *)theMessage { self.labelMessage.text = theMessage; [self.activityIndicator startAnimating]; self.hidden = NO; [self.superview bringSubviewToFront:self]; [self refreshPosition]; } - (void)hide { [self.activityIndicator stopAnimating]; self.hidden = YES; } - (void)refreshPosition { self.center = self.superview.center; } - (void)dealloc { self.labelMessage = nil; self.activityIndicator = nil; [super dealloc]; } @end
然后在你的view.h中
#import#import "LoadingIndicator.h" @interface YourView : UIView { LoadingIndicator *loadingIndicator; } @property(retain) LoadingIndicator *loadingIndicator; - (void)showLoadingIndicator:(NSString *)theMessage; - (void)hideLoadingIndicator; @end
在你的视图中
#import "YourView.h" @implementation YourView @synthesize loadingIndicator; - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { ... // Add your UIWebView etc ... // Loading Indicator LoadingIndicator *theLoadingIndicator = [[LoadingIndicator alloc] init]; theLoadingIndicator.hidden = YES; // Hidden by default self.loadingIndicator = theLoadingIndicator; [self addSubview:theLoadingIndicator]; [theLoadingIndicator release]; ... } return self; } - (void)showLoadingIndicator:(NSString *)theMessage { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [self.loadingIndicator performSelectorOnMainThread:@selector(show:) withObject:theMessage waitUntilDone:NO]; [pool release]; } - (void)hideLoadingIndicator { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [self.loadingIndicator performSelectorOnMainThread:@selector(hide) withObject:nil waitUntilDone:NO]; [pool release]; } - (void)dealloc { self.loadingIndicator = nil; [super dealloc]; } @end
最后在您的WebView委托中:
- (void) webViewDidStartLoad:(UIWebView *)webView { [(YourView *)self.view showLoadingIndicator:@"Loading"]; } - (void) webViewDidFinishLoad:(UIWebView *)webView { [(YourView *)self.view hideLoadingIndicator]; }
这看起来像这样:
在你的控制器.h
@property (nonatomic, retain) UIImageView *loadingImageView;
在你的控制器.m
@synthesize loadingImageView; - (void)viewDidLoad { ... UIImageView *theLoadingImageView = [[UIImageView alloc] initWithFrame:webView.frame]; theLoadingImageView.image = [UIImage imageNamed:@"your_image.png"]; self.loadingImageView = theLoadingImageView; [theLoadingImageView release]; ... } - (void) webViewDidStartLoad:(UIWebView *)webView { [self.view addSubview:self.loadingImageView]; } - (void) webViewDidFinishLoad:(UIWebView *)webView { [self.loadingImageView removeFromSuperview]; }
编辑1:
如果你想显示一个ActivityIndicatorView而不是一个静态图像,这里有一个我喜欢使用的小帮助类:
LoadingIndicator.h
#import@interface LoadingIndicator : UIView { UIActivityIndicatorView *activityIndicator; UILabel *labelMessage; } @property(retain) UIActivityIndicatorView *activityIndicator; @property(retain) UILabel *labelMessage; - (id)init; - (void)show:(NSString *)theMessage; - (void)hide; - (void)refreshPosition; @end
LoadingIndicator.m
#import "LoadingIndicator.h" #import@implementation LoadingIndicator @synthesize labelMessage, activityIndicator; - (id)init { if (self = [super initWithFrame:CGRectMake(25, 130, 270, 100)]) { // Vue self.backgroundColor = [UIColor blackColor]; self.alpha = 0.80; self.layer.cornerRadius = 5; // Label : message UILabel *theLabelMessage = [[UILabel alloc] initWithFrame:CGRectMake(15, 65, 240, 20)]; theLabelMessage.backgroundColor = [UIColor clearColor]; theLabelMessage.textColor = [UIColor whiteColor]; theLabelMessage.text = NSLocalizedString(@"Loading", @"Loading"); theLabelMessage.textAlignment = UITextAlignmentCenter; theLabelMessage.font = [UIFont boldSystemFontOfSize:16]; theLabelMessage.adjustsFontSizeToFitWidth = YES; self.labelMessage = theLabelMessage; [self addSubview:theLabelMessage]; [theLabelMessage release]; // Activity Indicator UIActivityIndicatorView *theActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; theActivityIndicator.frame = CGRectMake(115, 15, 40, 40); self.activityIndicator = theActivityIndicator; [self addSubview:theActivityIndicator]; [theActivityIndicator release]; } return self; } - (void)show:(NSString *)theMessage { self.labelMessage.text = theMessage; [self.activityIndicator startAnimating]; self.hidden = NO; [self.superview bringSubviewToFront:self]; [self refreshPosition]; } - (void)hide { [self.activityIndicator stopAnimating]; self.hidden = YES; } - (void)refreshPosition { self.center = self.superview.center; } - (void)dealloc { self.labelMessage = nil; self.activityIndicator = nil; [super dealloc]; } @end
然后在你的view.h中
#import#import "LoadingIndicator.h" @interface YourView : UIView { LoadingIndicator *loadingIndicator; } @property(retain) LoadingIndicator *loadingIndicator; - (void)showLoadingIndicator:(NSString *)theMessage; - (void)hideLoadingIndicator; @end
在你的视图中
#import "YourView.h" @implementation YourView @synthesize loadingIndicator; - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { ... // Add your UIWebView etc ... // Loading Indicator LoadingIndicator *theLoadingIndicator = [[LoadingIndicator alloc] init]; theLoadingIndicator.hidden = YES; // Hidden by default self.loadingIndicator = theLoadingIndicator; [self addSubview:theLoadingIndicator]; [theLoadingIndicator release]; ... } return self; } - (void)showLoadingIndicator:(NSString *)theMessage { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [self.loadingIndicator performSelectorOnMainThread:@selector(show:) withObject:theMessage waitUntilDone:NO]; [pool release]; } - (void)hideLoadingIndicator { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; [self.loadingIndicator performSelectorOnMainThread:@selector(hide) withObject:nil waitUntilDone:NO]; [pool release]; } - (void)dealloc { self.loadingIndicator = nil; [super dealloc]; } @end
最后在您的WebView委托中:
- (void) webViewDidStartLoad:(UIWebView *)webView { [(YourView *)self.view showLoadingIndicator:@"Loading"]; } - (void) webViewDidFinishLoad:(UIWebView *)webView { [(YourView *)self.view hideLoadingIndicator]; }
这看起来像这样: