问题
I created a webview programmatically and I am unable to enable scrolling in this view.
How do I enable scrolling?
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,40, 325, 1000)];
webView.contentMode = UIViewContentModeScaleAspectFit;
[webView setBackgroundColor:[UIColor clearColor]];
[webView loadHTMLString:html baseURL:nil];
[self.view insertSubview:webView atIndex:0];
Thanks In Advance !
回答1:
To enable scrolling,
webview.scrollView.scrollEnabled = TRUE;
and instead of writing this,
webView.contentMode = UIViewContentModeScaleAspectFit;
write this,
webview.scalesPageToFit = TRUE;
回答2:
Removing from Interface Builder and adding via code did for me. For some reason in IB it wasn't scrolling.
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, 320, 50)];
[self.view addSubview: webView];
回答3:
These are the possible solutions.
i) You webview may be large in size than you expected or your content is lesser than the webview size.
2) Implement following method.
func webViewDidFinishLoad(webView: UIWebView)
{
appDelegate.hideLoadingView()
let res: NSString = webView.stringByEvaluatingJavaScriptFromString("document.body.offsetHeight;")!
let h: CGFloat = CGFloat(res.integerValue)
agreementContentView.scrollView.contentSize = CGSizeMake(webView.frame.size.width, h + 50); // Offset if required
}
来源:https://stackoverflow.com/questions/10875971/cannot-scroll-in-uiwebview-in-ios