XCode在发布图像后阻止UIWebView闪烁的白色屏幕
问题描述:
我对XCode相当陌生 - 我正在设计一个带有单个视图的应用程序,该视图只显示加载我的jQuery移动网站的UIWebView。XCode在发布图像后阻止UIWebView闪烁的白色屏幕
我有Default.png和[email protected]文件启动图像的地方。当我在测试设备或模拟器上运行应用程序时,它会显示我的启动图像,然后在UIWebView加载第一页时闪烁白色屏幕。
我知道我可以更改背景颜色和不透明度,所以它会闪烁不同于白色的颜色,但我想完全防止闪光。我希望应用程序启动,显示启动图像,并在第一页完全加载之前不显示UIWebView。帮帮我?
答
@安德烈亚斯Volz的(和其他人想知道如何解决了白色的“闪光”的问题)
首先,添加name-of-loading-image.png
和[email protected]
文件到您的Xcode项目。常规形象应该是320×460,和@2x
形象应该是640×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];
}
答
要知道您的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];
}
可以使用-viewDidLoad方法在你的viewController的.M到继续显示您的启动图像。你也可以开始内容加载的网页视图:
- (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];
}
这就是我一直这样做了很长一段时间的方式。但是现在在iOS 8和动态加载屏幕中,没有name-of-loading-image.png在视图加载时加载。 – badweasel 2014-10-20 20:09:25