Take the 2-minute tour ×

I try to get a content editable UIWebView with a "normal" scrolling operation, so that when the text cursor is going to be hidden by the keyboard, the UIWebView scrolls to prevent that. I read the "Managing the Keyboard" document from iOS developer library and got no result, using the scrollView property of my UIWebView. I also found numerous tricks on the web, using ScrollView properties/methods or javascript commands but cannot obtain a normal scrolling operation, like any NSTextView does on MacOS for example. Do you know any solution to this problem?

[EDITED] Got the solution, and created a method fired by a NSTimer. To get the caret Y position, one should get the selection of the active element, and insert a dummy node. Then getting the node.offsetTop property gives the caretY. Do not forget the remove the node...

share|improve this question
Hi Denis, could you please give a more detailed solution of what you have done? I am also facing trouble with UIWebView, content editable and erratic scrolling behavior. –  Leo Natan Apr 19 '12 at 19:30
FYI: you can fill in the answer yourself and accept it :) –  Chad Jun 21 '12 at 14:31
Please consider answering your own question and accepting it to be a clearer Q&A for future users. Thank you. –  Daniel Aug 6 '12 at 4:08
add comment

1 Answer

Ok, here is the way Apple does it in their examples (I no longer remember what project name that was):

1.You register for keyboard notifications(the keyboard send a notification every time it shows up or it gets hidden):

[[NSNotificationCenter defaultCenter] addObserver:self
                                             name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                             name:UIKeyboardWillHideNotification object:nil];

2.Get the keyboard's size (you are interested in the height) and scroll if that point is not contained in the view:

- (void)keyboardWasShown:(NSNotification*)aNotification

NSDictionary* info = [aNotification userInfo];
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;

CGPoint point;

point = CGPointMake(0, activeTextField.frame.origin.y + activeTextField.frame.size.height);

if (!CGRectContainsPoint(aRect, point ))
    CGPoint scrollPoint = CGPointMake(0.0, activeTextField.frame.origin.y+activeTextField.frame.size.height-kbSize.height);
    [scrollView setContentOffset:scrollPoint animated:YES];

Don't forget do reverse things when hiding keyboard and remove the observers if you push to another view controller. This example is used with text fields on a UIScrollView but I'm sure you can easily adapt it.

Personally if I also manage the web side, I prefer to create the webpages 320px wide, set my webView sizeToFit, add it on top of a UIScrollView and manage the scrolling from there. For textFields, I simply get the container for the selected textField using javascript and get it's coordinates.

share|improve this answer
add comment

Your Answer


