Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent UIWebView to scroll when keyboard appears

Tags:

ios

uiwebview

I have a UIWebView presenting a page containing input fields ().

When the user touches the field to enter data, the UIWebView is scrolled up and left to make the field stay visible for the user. Well ok.

I don't mind it is scrolled up (because everything below my form is hidden by the keyboard) but I would like to prevent the view from being scrolled left.

Is there a way ( headers in the html page, or iOS code) to prevent this behavior ?

Thanks

like image 643
Lionel Tressens Avatar asked Apr 24 '12 17:04

Lionel Tressens


2 Answers

not beautiful but effective:

If you don't want your webView to scroll at all, you can implement the UIWebView's UIScrollViewDelegate and set the contentOffset to 0 on scrollViewDidScroll

aWebView.scrollView.delegate = self

func scrollViewDidScroll(scrollView: UIScrollView) {
    scrollView.contentOffset = CGPointMake(0, 0)
}
like image 171
Philipp Kinschel Avatar answered Sep 30 '22 05:09

Philipp Kinschel


I don't know how to prevent the scrolling entirely, but if you don't mind a little jitter, you can add this handler to the input field:

onfocus="setTimeout('window.scroll(0,0)',100)"
like image 35
Joshua Smith Avatar answered Sep 30 '22 05:09

Joshua Smith