的XCode防止UIWebView的闪白屏启动图像后(XCode Prevent UIWebView

2019-07-30 06:54发布

我是相当新的XCode的 - 我设计一个应用程序与只是显示加载我的jQuery Mobile的网站一个UIWebView的单一视图。

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

我知道我可以改变背景颜色和透明度,因此会闪烁不同颜色较白,但我想,以防止闪存完全。 我想该应用程序推出,显示启动图像,而不是显示UIWebView中,直到第一个页面完全加载。 救命?

Answer 1:

@安德烈亚斯Volz的(和其他人想知道如何解决了白色的“闪光”的问题)

首先,添加name-of-loading-image.pngname-of-loading-image@2x.png文件到您的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];

}


Answer 2:

要知道,当你的web视图加载完成后,你必须实现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];
}


文章来源: XCode Prevent UIWebView flashing white screen after Launch Image