【问题标题】:How to center the contents of a UIWebView on the iPhone如何在 iPhone 上将 UIWebView 的内容居中
【发布时间】:2010-05-27 17:53:57
【问题描述】:

我在UIWebView 中有一张地图图像。它默认加载在左上角。我希望它在UIWebView 的中心初始化。

有人知道怎么做吗?

谢谢!

【问题讨论】:

    标签: iphone cocoa-touch uiwebview center


    【解决方案1】:

    如果地图图像是页面中唯一的东西,它是否包含在 html 页面中?如果它至少包含在 body 标记中,您可以执行以下操作:

    NSString *bodyStyle = @"document.getElementsByTagName('body')[0].style.textAlign = 'center';";
    NSString *mapStyle = @"document.getElementById('mapid').style.margin = 'auto';";
    
    [webView stringByEvaluatingJavaScriptFromString:bodyStyle];
    [webView stringByEvaluatingJavaScriptFromString:mapStyle];
    

    让我知道您的 html 的结构究竟如何,然后我可以在必要时提供更多信息。

    【讨论】:

    • 不管它如何显示,与 UIWebView 交互的最简单(也是唯一)方法是通过 javascript
    【解决方案2】:

    另一种方法是将您的内容包装在 center-aligned div 标记中。

    <body>
    <div align="center">
    <!-- Your map here -->
    </div>
    </body>
    

    只要您可以修改传递给UIWebView 的html,这将适用于您的解决方案。它不需要任何 javascript 调用。但是,如果您希望在网页加载后按需注入样式表更新,则需要上述解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多