XCode 防止 UIWebView 在启动图像后闪烁白屏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11376495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XCode Prevent UIWebView flashing white screen after Launch Image
提问by adamdehaven
I am fairly new to XCode - I'm designing an app with a single view that just displays a UIWebView that loads my jQuery mobile web site.
我对 XCode 相当陌生 - 我正在设计一个带有单个视图的应用程序,该视图只显示一个加载我的 jQuery 移动网站的 UIWebView。
I have the Default.png and [email protected] files for the Launch images in place. When I run the app on my test device or in the emulator, it shows my Launch Image, and then flashes a white screen while the UIWebView loads the first page.
我有用于启动图像的 Default.png 和 [email protected] 文件。当我在我的测试设备或模拟器中运行应用程序时,它会显示我的启动图像,然后在 UIWebView 加载第一页时闪烁白屏。
I know that I can change the background color and opacity so it will flash a different color than white, but I would like to prevent the flash altogether. I would like the app to launch, show the Launch image, and not show the UIWebView until the first page has completely loaded. Help?
我知道我可以更改背景颜色和不透明度,因此它会闪烁与白色不同的颜色,但我想完全防止闪烁。我希望应用程序启动,显示启动图像,并且在第一页完全加载之前不显示 UIWebView。帮助?
回答by adamdehaven
@ Andreas Volz (and anyone else wondering how I solved the problem of the white "flash")
@ Andreas Volz(以及其他想知道我如何解决白色“闪光”问题的人)
First, add the name-of-loading-image.png
and [email protected]
files to your Xcode project. The regular image should be 320 x 460, and the @2x
image should be 640 x 920.
首先,将name-of-loading-image.png
和[email protected]
文件添加到您的 Xcode 项目中。常规图像应为 320 x 460,@2x
图像应为 640 x 920。
Then, in my ViewController.h
file I added these properties:
然后,在我的ViewController.h
文件中,我添加了这些属性:
@property (nonatomic, retain) UIWebView *webView;
@property(nonatomic, strong) UIImageView *loadingImageView;
@end
And then in my ViewController.m
file I added this:
然后在我的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];
}
回答by jimmyb
To know when your webView has finished loading, you must implement the UIWebViewDelegate protocol.
要知道您的 webView 何时完成加载,您必须实现 UIWebViewDelegate 协议。
In your viewController's .h file:
在您的 viewController 的 .h 文件中:
@interface viewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
}
@end
Then in your viewController's .m file, add this method:
然后在你的 viewController 的 .m 文件中,添加这个方法:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"content loading finished");
// Display your webView
[self.view addSubview:webView];
}
You can use the -viewDidLoad method in your viewController's .m to continue displaying your launch image. You can also begin the loading of content for your 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];
}