Cannot scroll in UIWebView in iOS

一世执手 提交于 2019-11-30 23:20:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!