1

我对 XCode 相当陌生 - 我正在设计一个具有单个视图的应用程序,该视图仅显示加载我的 jQuery 移动网站的 UIWebView。

我有用于启动图像的 Default.png 和 Default@2x.png 文件。当我在测试设备或模拟器中运行应用程序时,它会显示我的启动图像,然后在 UIWebView 加载第一页时闪烁白屏。

我知道我可以改变背景颜色和不透明度,所以它会闪烁不同于白色的颜色,但我想完全防止闪烁。我希望应用程序启动,显示启动图像,并且在第一页完全加载之前不显示 UIWebView。帮助?

4

2 回答 2

4

@ Andreas Volz(还有其他想知道我如何解决白色“闪光”问题的人)

首先,将name-of-loading-image.pngname-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];

}
于 2012-07-26T12:48:20.690 回答
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];
}
于 2012-07-13T22:03:07.153 回答