【问题标题】:XCode Prevent UIWebView flashing white screen after Launch ImageXCode防止UIWebView在启动图像后闪烁白屏
【发布时间】:2012-07-07 18:08:52
【问题描述】:

我是 XCode 的新手——我正在设计一个具有单个视图的应用程序,它只显示一个加载我的 jQuery 移动网站的 UIWebView。

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

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

【问题讨论】:

    标签: xcode ios5 uiwebview


    【解决方案1】:

    @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];
    
    }
    

    【讨论】:

    • 这也是我长期以来一直这样做的方式。但是现在有了 iOS 8 和动态加载屏幕,没有 name-of-loading-image.png 可以加载到视图中。
    【解决方案2】:

    要知道您的 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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      相关资源
      最近更新 更多