views:

483

answers:

4

Hi,

I am a beginner at iPhone development and am hoping someone can help me with my question.

I have a UIWebView displaying a web page. If the user taps inside a textbox on the web page then the keyboard pops up. This is great, but it hides the field that the user tapped on. I have looked around and found code samples to deal with this, but none that specifically deal with the UIWebView. I have implemented UIKeyboardDidShowNotification and UIKeyboardDidHideNotification but am not sure how to resize the UIWebView properly. I have tried putting the UIWebView in a UIScrollView but not had any success with that. The code below seems to adjust the UIWebView but won't let it scroll to the field.

-(void) keyboardDidShow: (NSNotification *)notif{
    if (keyboardShown) return;

    NSLog(@"Keyboard Show");
    NSDictionary *info = [notif userInfo];
    NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;

    CGRect viewFrame = webBrowser.frame;
    viewFrame.size.height -= keyboardSize.height;
    webBrowser.frame = viewFrame;
    keyboardShown = YES;
}

-(void) keyboardDidHide: (NSNotification *)notif{
    NSLog(@"Keyboard hide");
    keyboardShown = NO;

}

Hopefully someone can help me or point me in the right direction.

Thanks!

Pete

A: 

Hi, Any Luck ? I am facing the same problem...

Neelam Roy
Hi, Unfortunately no luck yet. I think I have two problems to figure out.1. When I resize the UIWebView control and give it an arbitrary height (for testing) such as:viewFrame.size.height = 200; // for testing // -= keyboardSize.height;webBrowser.frame = viewFrame;I see the control resize on the screen, however I am unable to scroll the webview to the bottom of the document via touch control. Perhaps I need to resize something else to make this work?2. Once I can get the resized control to scroll the entire length of the document I need it to scroll to the selected control.
Pete
pete - not sure what you mean by 'scroll' but a webview doesn't scroll. you need a uiscrollview (or descendant of) for that. also, a line like ' viewFrame.size.height -= keyboardSize.height;' shouldn't compile because you're not assignig to an ivalue. i would check out christopher's solution
Remover
+1  A: 

I think you need to change the center position of the view rather than the height of it if you want to do it manually. That is, set a new y position for webBrowser.frame which is minus keyboardSize.height.

Remover
A: 

You could try if making the bounds of the UIWebView smaller would solve the problem.

CGRect viewFrame = webBrowser.frame;
viewFrame.size.height -= keyboardSize.height;
webBrowser.bounds = viewFrame;
schaechtele
+1  A: 

You can make a UIAnimation that will move the UIWebview up with the keyboard and then use its delegate to make another UIAnimation when typing is over to move back the UIWebview to its original location.

Christophe Konig