xcode 如何控制 UIWebView 的颜色(在内容加载之前)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2491654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:13:28  来源:igfitidea点击:

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

iphoneobjective-cxcodeuiviewuiwebview

提问by RexOnRoids

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, making 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?

我试图在其内容加载之前有一个黑色的 UIWebView。在 IB 中,我尝试将 UIWebView 的背景颜色设为黑色,以及使其具有透明背景并将其父视图的背景设为黑色。两者都不起作用。当我的 UIWebView 加载它的背景时,它是白色的。我怎样才能解决这个问题?

*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.

*我希望这不会归结为必须首先加载一个 html 字符串(在加载我的实际 Web 内容之前)以将背景设置为黑色 vis CSS。

回答by cornellouis

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 .

我这样做的方法是将 UIWebView 放在黑色视图上。然后,我们将网页和webview设置为清晰。适用于 iOS 4.2。

On the superview:

在超级视图上:

view.backgroundColor = [UIColor blackColor];

On the webView:

在网络视图上:

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

In the Web HTML:

在网页 HTML 中:

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

回答by smileBot

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.

这将显示背景颜色,一旦 webview 加载,它就会很高兴地消失。

回答by zonble

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.

实际设置背景颜色并不能使网页视图全黑。网页的内容呈现在网页视图中的子视图中,网页视图包含许多未记录的子视图,包括允许您滚动内容的 UIScrollView,而 UIScrollView 包含绘制网页内容的 UIWebDocumentView。它们位于您的 web 视图的背景之上,因此它们覆盖了背景,您可以看到的是 UIWebDocumentView 并且设置背景颜色不能使您的 web 视图全黑。

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.

我想解决方案是,您可以尝试在 Web 视图上放置一个黑色视图,并在调用webViewDidFinishLoad:或等委托方法时隐藏该视图webViewDidStartLoad:

回答by Jaywant Khedkar

Try This it work for me,

试试这对我有用,

Follow below steps ,

按照以下步骤,

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

设置 UIview 和 webview opaque = NO 并且背景颜色为白色

enter image description here

在此处输入图片说明

Add below line of code before load of web view,

在加载 Web 视图之前添加以下代码行,

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

Hope so this is help for someone

希望这是对某人的帮助

回答by godblessstrawberry

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

为了消除这个白色闪烁,我只将 backgroundColor 更改为我的视图颜色。我的代码使用紫色来显示更一般的示例。黑色被简单地用作UIColor.black

editing loadViewis not working for me, but viewWillAppearworked fine

编辑loadView对我不起作用,但viewWillAppear工作正常

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

回答by russes

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

对于 iOS 7,这是另一个示例。“Self”是一个 UIViewController,它被推送到导航控制器堆栈上:

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

回答by Beppex

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)

在 WebView 上添加一个带有自定义颜色背景(我使用黑色)的 UIView(我使用 blackView),然后在加载 WebView 时显示它并在加载时隐藏。WebView 必须具有透明背景,根视图必须具有与您的自定义颜色相同的背景颜色(在我的情况下为黑色)

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

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

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

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

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

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