我对XCode还不熟悉 - 我正在设计一个带有单一视图的应用程序,它只显示一个加载我的jQuery移动网站的UIWebView。
我有适用于启动图像的Default.png和Default@2x.png文件。当我在我的测试设备或模拟器中运行应用程序时,它会显示我的启动图像,然后在UIWebView加载第一页时闪烁白屏。
我知道我可以改变背景颜色和不透明度,因此它会闪烁不同于白色的颜色,但我想完全阻止闪光。我希望应用程序启动,显示启动图像,并且在第一页完全加载之前不显示UIWebView。帮助
答案 0 :(得分:4)
首先,将name-of-loading-image.png
和name-of-loading-image@2x.png
文件添加到Xcode项目中。常规图像应为320 x 460,@2x
图像应为640 x 920。
然后,在我的ViewController.h
文件中,我添加了以下属性:
@property (nonatomic, retain) UIWebView *webView;
@property(nonatomic, strong) UIImageView *loadingImageView;
@end
然后在我的ViewController.m
文件中我添加了这个:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController;
@synthesize webView;
@synthesize loadingImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];
//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:@"name-of-loading-image.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"name-of-loading-image.png"],
nil];
[self.view addSubview:loadingImageView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Remove loading image from view
[loadingImageView removeFromSuperview];
}
答案 1 :(得分:1)
要知道webView何时完成加载,您必须实现UIWebViewDelegate协议。
在viewController的.h文件中:
@interface viewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
}
@end
然后在viewController的.m文件中添加以下方法:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"content loading finished");
// Display your webView
[self.view addSubview:webView];
}
您可以在viewController的.m中使用-viewDidLoad方法继续显示启动图像。您也可以开始加载webView的内容:
- (void)viewDidLoad {
[super viewDidLoad];
// continue to display launch image
UIImage *launchImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"launchImage.png" ofType:nil ]];
UIImageView *launchImageView = [[[UIImageView alloc] initWithImage:launchImage] autorelease];
[self.view addSubview:launchImageView];
// now setup the base view for the webView controller
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blueColor];
// for rotation
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
//create a frame to size and place the web view
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
self.webView = aWebView;
//aWebView.scalesPageToFit = YES;
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//determine the path the to the index.html file in the Resources directory
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
}