【问题标题】:Change contents background color in UIWebView in Xcode在 Xcode 中的 UIWebView 中更改内容背景颜色
【发布时间】:2013-02-01 10:10:19
【问题描述】:

我创建了一个简单的UIWebview 控件,在其中我从 html 文件加载内容,但我想更改该 html 页面的背景而不是我的 web 视图。

对此有任何想法吗?

【问题讨论】:

    标签: ios uiwebview webview


    【解决方案1】:

    你有两个选择:

    1. 修改 HTML 以使用您选择的背景颜色
    2. 向 HTML 添加一个 javascript 函数,将背景颜色设置为传递的任何内容,然后使用 stringByEvaluatingJavaScriptFromString 调用它

    示例JS函数:

    function changeBackground(color){
    document.getElementByName("body").style.background="#"+color;
    }
    

    示例ios调用代码:

    [self.webView stringByEvaluatingJavaScriptFromString([NSString stringWithFormat:@"changeBackground(\"%@\");",yourColor];
    

    【讨论】:

      【解决方案2】:

      设置UIWebView背景颜色和不透明:NO

      [webView setBackgroundColor:[UIColor clearColor]];
      [webView setBackgroundColor:[UIColor purpleColor]];
      
      [webView setOpaque:NO];
      

      【讨论】:

        【解决方案3】:

        你需要在 html body 设置背景颜色。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-09-21
          • 2012-02-29
          • 1970-01-01
          • 1970-01-01
          • 2014-08-17
          • 2021-12-13
          • 2016-11-28
          相关资源
          最近更新 更多