开发者

How to control the color of a UIWebView (before contents load)?

I am trying to have a BLACK colored UIWebView before its contents load. In IB, i tried both making the background color of the UIWebView black, as well as, m开发者_Python百科aking it have a transparent background and have its parent view's background be black. Both don't work. When my UIWebView loads the background of it is white. How can I fix this?

*I hope this does not boil down to having to load an html string first (before loading my actual web content) to set the background to black vis CSS.


The way I did this was to put the UIWebView on a black colored view. Then, we set the web page and the webview to be clear. Works in iOS 4.2 .

On the superview:

view.backgroundColor = [UIColor blackColor];

On the webView:

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

In the Web HTML:

<body style="background-color:transparent;">


Actually setting the background color cannot make the web view entire black. The content of a web page is presented in a subview within the web view, a web view contains many undocumented subviews, including a UIScrollView which allows you to scroll the contents, and a UIScrollView contains UIWebDocumentView, which draws the web contents. They are above the background of your web view, so they cover the background, what you can see is the UIWebDocumentView and setting the background color cannot make your web view entire black.

I guess the solution is, you may try to place a black view over your web view, and hide the view when the delegate methods such as webViewDidFinishLoad: or webViewDidStartLoad: are called.


Actually, this is really simple:

[self.webView setOpaque:NO];
self.webView.backgroundColor = [UIColor purpleColor];

This will show the background color, and as soon as the webview loads it will, happily, disappear.


Try This it work for me,

Follow below steps ,

Set UIview and webview opaque = NO and background Color is white

How to control the color of a UIWebView (before contents load)?

Add below line of code before load of web view,

  _webViewOutlet.backgroundColor = [UIColor colorWithWhite:1 alpha:1]; 

Hope so this is help for someone


to remove this white flicker I only change backgroundColor to the color of my view. my code uses purple to show more general example. black is used simply as UIColor.black

editing loadView is not working for me, but viewWillAppear worked fine

override func viewWillAppear(_ animated: Bool) {
    ...
    webView.isOpaque = false
    webView.backgroundColor = UIColor(red: 41/255, green: 45/255, blue: 91/255, alpha: 1)
    ...
}


For iOS 7, here's another example. "Self" is a UIViewController that's been pushed onto the Navigation Controller stack:

- (void) loadView
{
    UIWebView *webView = [[UIWebView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];      // iOS 7 - make the web view the entire display

// -- http://stackoverflow.com/questions/2491654/how-to-control-the-color-of-a-uiwebview-before-contents-load --

    webView.backgroundColor = [[Settings sharedInstance] backgroundColor];
    webView.opaque          = NO;                                               // setting this to NO fixes the background color problem
    webView.delegate        = self;

    self.view = webView;

    NSBundle      *thisBundle = [NSBundle mainBundle];
    NSString            *path = [thisBundle pathForResource: @"Instructions" ofType: @"html"];
       NSURL *instructionsURL = [[NSURL alloc] initFileURLWithPath: path];

    [webView loadRequest: [NSURLRequest requestWithURL: instructionsURL]];
}


Add an UIView (I used blackView) with your custom color background (I used black color) over the WebView, and then show it on load WebView and hide when loaded. WebView must have background transparent and the root view must have the same background color of your custom color (black in my case)

  • in viewDidLoad() view.backgroundColor = UIColor.blackColor() webView.opaque = false

  • in webViewDidStartLoad(webView: UIWebView) blackView.alpha = 1

  • in webViewDidFinishLoad(webView: UIWebView) blackView.alpha = 0

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜