2 for 2012 for_2012 于 2013.06.07 11:27 提问

webView内容水平和垂直都居中

需要水平和垂直居中webView的内容,我可以这样实现水平居中

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *bodyStyle = @"document.getElementsByTagName('body')[0].style.textAlign = 'center';";
    [self.webView stringByEvaluatingJavaScriptFromString:bodyStyle];
}

但是不知道怎么实现垂直居中?

2个回答

MrsSeven7
MrsSeven7   2013.06.07 14:00
NSString *bodyStyleVertical = @"document.getElementsByTagName('body')[0].style.verticalAlign = 'middle';";
NSString *bodyStyleHorizontal = @"document.getElementsByTagName('body')[0].style.textAlign = 'center';";
NSString *mapStyle = @"document.getElementById('mapid').style.margin = 'auto';";

[webView stringByEvaluatingJavaScriptFromString:bodyStyleVertical];
[webView stringByEvaluatingJavaScriptFromString:bodyStyleHorizontal];
[webView stringByEvaluatingJavaScriptFromString:mapStyle];

试试看

hjqiaho
hjqiaho   2016.06.16 12:33

@" var oantzone=document.getElementsByTagName('body')[0];var h=oantzone.offsetHeight;oantzone.style.position='absolute';oantzone.style.top='50%';oantzone.style.marginTop=-(h/2)+'px';"

Csdn user default icon
上传中...
上传图片
插入图片