Emi*_*mil 9 javascript iphone objective-c uiwebview uiscrollview
我使用UIWebView
的是以HTML字符串的形式显示内容 - 不是网站,高于iPhone的屏幕,而不需要滚动webView本身,将其留给父滚动视图.
为了实现这一点,我需要一种方法来获取总文档大小,包括可滚动区域,以设置webView的高度.我尝试了许多不同的Javascript解决方案:
(document.height !== undefined) ? document.height : document.body.offsetHeight // Returns height of UIWebView
document.body.offsetHeight // Returns zero
document.body.clientHeight // Returns zero
document.documentElement.clientHeight // Returns height of UIWebView
window.innerHeight // Returns height of UIWebView -2
document.body.scrollHeight // Returns zero
Run Code Online (Sandbox Code Playgroud)
有没有真正有效的解决方案?
当前(非工作)代码:
[[[self.singlePost.contentText subviews] lastObject] setScrollEnabled:NO];
int content_height = [[self.singlePost.contentText stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue];
NSLog(@"Content_height: %d", content_height);
CGRect rect = self.singlePost.contentText.frame;
rect.size.height = content_height;
self.singlePost.contentText.frame = rect;
Run Code Online (Sandbox Code Playgroud)
And*_*sky 18
在iOS 5.0及更高版本中无需使用Javascript - 您可以直接访问其scrollView:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGFloat contentHeight = webView.scrollView.contentSize.height;
// ....
}
Run Code Online (Sandbox Code Playgroud)
获取webView内容的总高度.
kar*_*rim 11
当我尝试使用我的代码时,我发现,
(1) NSLog(@"webView height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]);
(2) NSLog(@"webView height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.height"]);
Run Code Online (Sandbox Code Playgroud)
(1)
返回高度小于原始高度.但要(2)
返回实际高度.我的建议是得到两者Max
中的两个.
- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
NSLog(@"Body height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]);
NSLog(@"Doc height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.height"]);
NSString * javaScript = [NSString stringWithFormat:@"document.getElementById('%@').clientHeight", kDivID];
NSLog(@"Div height: %@",[webView stringByEvaluatingJavaScriptFromString:javaScript]);
[self reLayout];
}
- (CGFloat) getWebViewPageHeight {
CGFloat height1 = [[webView stringByEvaluatingJavaScriptFromString: @"document.height"] floatValue];
CGFloat height2 = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] floatValue];
return MAX(height1, height2);
}
Run Code Online (Sandbox Code Playgroud)
Body height: 485
Doc height: 509
Div height: 485
Run Code Online (Sandbox Code Playgroud)
小智 6
你在哪里打电话给你的代码?对我来说,如果在loadHTMLString方法之后立即调用它,则返回0.如果我在(void)webViewDidFinishLoad:(UIWebView*)theWebView委托中调用它,我会得到一个有效值.
- (void)loadHTML: (NSString *)html {
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
webView.delegate = self;
NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL: resourceURL];
NSLog(@"height: %d", [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue]); // returns 0
}
- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
NSLog(@"height: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]); //return 2166
}
Run Code Online (Sandbox Code Playgroud)
我采用了稍微不同的方法,创建了一个我可以关闭以获得大小的<DIV>,因为我在检查文档/正文时没有运气.
这是MonoTouch C#,但在Objective-C中应该可以正常工作:
private const string DIV_ID = "_uiwebview_";
LoadHtmlString("<body style='background-color:#yourcolor; padding:0px; margin:0px'>" +
"<div style='width:" + Frame.Width + "px; padding:0px; margin:0px; font-family:" +
font.Name + "' id=" + DIV_ID + ">" + html + "</div></body>", base_url);
Run Code Online (Sandbox Code Playgroud)
并在LoadFinished中获取高度:
string sheight = EvaluateJavascript("document.getElementById('" + DIV_ID +
"').clientHeight");
Run Code Online (Sandbox Code Playgroud)
@Jesse,这可以在使用HTML进行更新时生成,该HTML将生成比以前显示的更小的尺寸.
归档时间: |
|
查看次数: |
23572 次 |
最近记录: |