2012-07-07 59 views
1

我對XCode相當陌生 - 我正在設計一個帶有單個視圖的應用程序,該視圖只顯示加載我的jQuery移動網站的UIWebView。XCode在發佈圖像後阻止UIWebView閃爍的白色屏幕

我有Default.png和[email protected]文件啓動圖像的地方。當我在測試設備或模擬器上運行應用程序時,它會顯示我的啓動圖像,然後在UIWebView加載第一頁時閃爍白色屏幕。

我知道我可以更改背景顏色和不透明度,所以它會閃爍不同於白色的顏色,但我想完全防止閃光。我希望應用程序啓動,顯示啓動圖像,並在第一頁完全加載之前不顯示UIWebView。幫幫我?

回答

4

@安德烈亞斯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]; 

} 
+0

這就是我一直這樣做了很長一段時間的方式。但是現在在iOS 8和動態加載屏幕中,沒有name-of-loading-image.png在視圖加載時加載。 – badweasel 2014-10-20 20:09:25

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

} 

可以使用-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]; 
}