如何在iPhone上集中UIWebView的内容

时间:2010-05-27 17:53:58

标签: iphone cocoa-touch uiwebview center

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

有谁知道怎么做?

谢谢!

2 个答案:

答案 0 :(得分:6)

如果地图图像是页面中唯一的内容,它是否包含在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是如何构建的,然后我可以在必要时提供更多信息。

答案 1 :(得分:2)

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

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

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