Cannot scroll in UIWebView in iOS
Asked Answered
S

3

13

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 !

Spiel answered 4/6, 2012 at 3:20 Comment(4)
Ummm, if you have the content set to scale & fit (e.g. "UIViewContentModeScaleAspectFit"), wouldn't the whole page fit on screen and not scroll?Seacoast
@MichaelDautermann..I tried disabling "UIViewContentModeScaleAspectFit" but I still cannot scroll. webView just bouncesSpiel
@skokal01 that means that all the content is visible on the screen so there is nothing to scroll toBenevolent
@Otium...I guarantee that there is lot of content. BTW I changed my code to create UIWebView from interface builder and now I am able to scroll perfectly. I am wondering what I have missed in my code.Spiel
C
23

To enable scrolling,

webview.scrollView.scrollEnabled = TRUE;

and instead of writing this,

webView.contentMode = UIViewContentModeScaleAspectFit;

write this,

webview.scalesPageToFit = TRUE;
Collum answered 4/6, 2012 at 11:53 Comment(9)
@krunal...It didnot work. Now my html page is shrunk to 1/4th size of the screen and I can't even zoom in :(Spiel
Please see the screenshot at UIWebView ScreenshotSpiel
size of your web view is not proper.. chk that, it should be webview.frame=CGRectMake(0, 0, 770, 960);Collum
@krunal..nope it still doesn't scroll.Spiel
try this, webview.frame=CGRectMake(0, 0, 100, 100); and remove this line webview.scalesPageToFit = TRUE; & thn tell me what happens..Collum
@Krunal...After resizing the UIWebView correctly it worked UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; webView.scrollView.scrollEnabled = TRUE; BTW if I set webView.scalesPageToFit, page shrinks to 1/4th of the screen size.Spiel
@krunal...I was about to mark the question as answered. anyways thxSpiel
I had this same problem and in my case I was trying to add the UIWebView to a parent UIView that was smaller than my webView. I found this easily by setting the borderWidth on the parent view.Stimulus
I think you mean YES not TRUE. This is objective c! :)Policy
R
0

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
}
Riggs answered 9/12, 2015 at 7:0 Comment(0)
M
-1

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];
Marin answered 19/9, 2014 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.