Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set UIWebView Content not to move when keyboard is shown

I've done large enough research but couldn't find answer to my question.

Suppose I have a webView in which I have some text fields, some of them are placed on the bottom of screen so that when keyboard appears it should hide that fields. After keyboard appears the content of webView slides up in order to make that fields visible. The problem is that I DON'T want the content to slide up.

Question is: How can I disable that feature of webview , or somehow make the content not scroll up.???

Thanks, any help would be appreciated.

like image 452
Garnik Avatar asked Apr 10 '13 12:04

Garnik


1 Answers

If you want to disable ALL scrolling, including the auto-scroll when you navigate between form fields, setting webView.scrollView.scrollEnabled=NO doesn't quite cover everything. That stops normal tap-and-drag scrolling, but not the automatic bring-field-into-view scrolling when you navigate around a web form.

Also, watching for UIKeyboardWillShowNotification will let you prevent scrolling when the keyboard appears, but that will do nothing if the keyboard is already up from editing a different form field.

Here's how to prevent ALL scrolling in three simple steps:

1) After you create the UIWebView, disable normal scrolling:

myWebView.scrollView.scrollEnabled = NO;

2) Then register your view controller as the scrollView's delegate:

myWebView.scrollView.delegate = self;

(And make sure to add <UIScrollViewDelegate> to your class's @interface definition to prevent compiler warnings)

3) Capture and undo all scroll events:

// UIScrollViewDelegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    scrollView.bounds = myWebView.bounds;
}
like image 112
Richard Connamacher Avatar answered Nov 15 '22 13:11

Richard Connamacher