转自:http://www.cocoachina.com/bbs/simple/?t41614.html


- (void) registerForKeyboardNotificati*****{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];

}


- (void) keyboardWasShown:(NSNotification *) notif{
    NSDictionary *info = [notif userInfo];
    
    NSValue *value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    CGRect scrollViewFrame= [scrollView frame];
    scrollViewFrame.size.height -= keyboardSize.height;
    scrollView.frame = scrollViewFrame;
    [scrollView scrollRectToVisible:inputElementFrame animated:YES];
    keyboardWasShown = YES;
}

- (void) keyboardWasHidden:(NSNotification *) notif{
    NSDictionary *info = [notif userInfo];
    
    NSValue *value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    CGRect scrollViewFrame= [scrollView frame];
    scrollViewFrame.size.height += keyboardSize.height;
    scrollView.frame = scrollViewFrame;
    keyboardWasShown = NO;
    
}