键盘显示事件后,我的 UIScrollview 出现问题,显然我的 UITextField 像这样向上移动:
http://i.stack.imgur.com/ccBSp.png
这是在显示键盘之后:
http://i.stack.imgur.com/X6NU2.png
我完全按照示例进行,这是我的代码:
-(void) keyboardDidShow: (NSNotification *)notif {
if (keyboardVisible) {
NSLog(@"Keyboard is already visible. Ignoring notofication.");
return;
}
//The keyboard wasn't visible before
NSLog(@"Resizing smaller for keyboard");
// Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = self.view.frame;
viewFrame.size.height -= keyboardSize.height;
scrollView.frame = viewFrame;
keyboardVisible = YES;}
-(void) keyboardDidHide: (NSNotification *)notif {
if (!keyboardVisible) {
NSLog(@"Keyboard is already hidden. Ignoring notification.");
return;
}
// The keyboard was visible
NSLog (@"Resizing bigger with no keyboard");
//Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Reset the height of the scroll view to its original value
CGRect viewFrame = self.view.frame;
viewFrame.size.height += keyboardSize.height;
scrollView.frame = viewFrame;
keyboardVisible = NO;}