How to Prevent Initial White Flash When Showing a Uiwebview

How to prevent initial white flash when showing a UIWebView?

Swift:

webView.isOpaque = false
webView.backgroundColor = UIColor.clear

Objective-C:

webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];

Preloading a UIWebView, avoiding white screen flash

I have tested it... I'm sending the two method that I have used...

 - (void)viewDidLoad {
[super viewDidLoad]; //objWebView is the outlet of UIWebView
[objWebView loadHTMLString:@"<html><body style=\"background-color:black;\"></body></html>" baseURL:nil];

//the more the delay the errors will be less so within 0.1-0.3 would be fine
[self performSelector:@selector(loadURL:) withObject:nil afterDelay:0.1];
}

-(void)loadURL:(id)sender{
[objWebView stopLoading]; //added this line to stop the previous request
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[objWebView loadRequest:req];
}

here I'm performing the request after 0.1 sec, other wise it will look white as in your case. Or you can give your delay time depending upon the time.. Cheers :)

XCode Prevent UIWebView flashing white screen after Launch Image

@ Andreas Volz (and anyone else wondering how I solved the problem of the white "flash")

First, add the name-of-loading-image.png and name-of-loading-image@2x.png files to your Xcode project. The regular image should be 320 x 460, and the @2x image should be 640 x 920.

Then, in my ViewController.h file I added these properties:

@property (nonatomic, retain) UIWebView *webView;
@property(nonatomic, strong) UIImageView *loadingImageView;
@end

And then in my ViewController.m file I added this:

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

}

UIWebView white flashing when load page

Try my code will help you!

Step 1: Fetch UIWebView and UIActivityIndicatorView to your view controller.
Step 2: Delegate UIWebView to viewController and Create outlets for UIWebView and UIActivityIndicatorView.

Step 3: Select UIActivityIndicatorView on view controller then go to Attributes Inspector ->Activity Indicator View -> check Animating and Hides when Stopped on Behaviour

Step 4: Add Below codes to your ViewController.

import UIKit

class ViewController: UIViewController,UIWebViewDelegate{

@IBOutlet var loader: UIActivityIndicatorView!
@IBOutlet var webView: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false //to avoid auto scrolling.
functionOfWebView()

}

func functionOfWebView()
{
let URL = NSURL(string: "http://www.google.com")
//let URL = NSBundle.mainBundle().URLForResource("index", withExtension: "html") //For local html file(index.html) with local file hyperlink(file.html) see on video tutorial
let request = NSURLRequest(URL: URL!)
webView.loadRequest(request)
}

func webViewDidStartLoad(webView: UIWebView)
{
loader.startAnimating()
}

func webViewDidFinishLoad(webView: UIWebView)
{
loader.stopAnimating()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

}

In Swift 3.0

import UIKit

class ViewController: UIViewController,UIWebViewDelegate{

@IBOutlet var loader: UIActivityIndicatorView!
@IBOutlet var webView: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()

self.automaticallyAdjustsScrollViewInsets = false //to avoid auto scrolling.
functionOfWebView()
}

func functionOfWebView()
{
let URL = NSURL(string: "http://www.google.com")
//let URL = Bundle.main.url(forResource: "index", withExtension: "html") //For local html file(index.html) with local file hyperlink(file.html) see on video tutorial
let request = NSURLRequest(url: URL! as URL)
webView.loadRequest(request as URLRequest)
}

func webViewDidStartLoad(_ webView: UIWebView)
{
loader.startAnimating()
}

func webViewDidFinishLoad(_ webView: UIWebView)
{
loader.stopAnimating()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Sample Image

if you confuse or for Using Local html files then see this Youtube tutorial

UIWebView white screen on every launch while preloading all tabs

This post made my day. I am now using NSNotificationCenter for this.

iphone uiwebview initial white view

One thing you can try is put a UIView with the color you want and position it above the UIWebView with the same width and height. Then in the webViewDidFinishLoad method, set the UIView to hidden.

iPhone app: avoiding white screen after splash screen. Let splash screen linger, hide it after UIWebview loads? Splash screen not hiding properly

Here's a way to achieve it, get rid of all the code in your AppDelegate first of all. In your root view controller add an instance variable of class UIImageView called "splash".

Now in the rootViewController.m:

-(void) viewWillAppear:(BOOL) animated {

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

self.view.userInteractionEnabled = NO;

splash = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

splash.image = [UIImage imageNamed:@"Default.png"];
[self.view addSubview:splash];
});
}

Now in your webView load completion callback method/block

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

[splash removeFromSuperView];
});

so the dispatch_once makes sure the method will run once and only once in the life time of the application.

EDIT:

To get your callback:

in viewController.h -> viewC : UIViewController < UIWebViewDelegate >

in viewController.m

-(void) viewDidLoad{

CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];

webView.delegate = self;

[webView setBackgroundColor:[UIColor clearColor]];
NSString *urlAddress = @"http://www.cnn.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}

then

-(void) webViewDidFinishLoad:(UIWebView *)webView {

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

[splash removeFromSuperView];
});
}


Related Topics



Leave a reply



Submit