ios UIWebView 背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6605140/
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-08-30 20:39:02  来源:igfitidea点击:

UIWebView background color

iosuiwebview

提问by Namhcir

I am loading an HTML string into a UIWebView in order to be able to view rich text. So far, so good. But I have one small problem: in my Nib file, I set the background attribute to green. However, when it is displayed, the background is white. Then, in the class file, I added the following

我正在将 HTML 字符串加载到 UIWebView 中,以便能够查看富文本。到现在为止还挺好。但是我有一个小问题:在我的 Nib 文件中,我将背景属性设置为绿色。但是,当它显示时,背景是白色的。然后,在类文件中,我添加了以下内容

[myWebView setBackgroundColor:[UIColor greenColor]];
    [myWebView loadHTMLString:myString baseURL:nil];

However, the background is still white. I even tried reversing the order, but still comes out white background.

但是,背景仍然是白色的。我什至尝试颠倒顺序,但仍然出现白色背景。

There is more text than the size of the UIWebView. I noticed that when I scroll down past the end of the text, the background is then green. How do I get the background for the text itself to be green?

文本比 UIWebView 的大小还多。我注意到当我向下滚动到文本末尾时,背景是绿色的。如何使文本本身的背景变为绿色?

Could you please advise me as to what I am doing wrong? Muchly appreciated.

你能告诉我我做错了什么吗?非常感谢。

回答by Kal

Set the background color in your html itself using css markup.

使用 css 标记在 html 本身中设置背景颜色。

Or, set the webview'sopaqueproperty to NO.. and set the background of the view underneath to green.

或者,将webview 的opaque属性NO设置为.. 并将下方视图的背景设置为绿色。

UIWebViewthemselves don't seem to understand background color-- since they are rendering documents.

UIWebView他们自己似乎不理解背景颜色——因为他们正在渲染文档。

回答by ashokdy

Use this an addition to your code

将此作为代码的补充

[myWebView setBackgroundColor:[UIColor greenColor]];
[myWebView setOpaque:NO];
[myWebView loadHTMLString:myString baseURL:nil];

回答by Nilesh Patel

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

回答by Museer Ahamad Ansari

Tested in Xcode8 and Swift 3

在 Xcode8 和 Swift 3 中测试

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

回答by harish babu

UIWebView *webview =[[UIWebView alloc];

[webview setBackgroundColor:[UIColor whiteColor]];