1

键盘显示事件后,我的 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;}
4

1 回答 1

0

您是否尝试过使用keyboardWasShown 和keyboardWasHidden 而不是WillShow 和WillHide?另外,我注意到您在显示和隐藏中都使用了“UIKeyboardBoundsUserInfoKey”。尝试“UIKeyboardFrameBeginUserInfoKey”和“UIKeyboardFrameEndUserInfoKey”

于 2012-12-19T09:48:56.350 回答