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
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.
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.
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;
精彩评论