//在遇到有输入的情况下。由于现在键盘的高度是动态变化的。中文输入与英文输入时高度不同。所以输入框的位置也要做出相应的变化 #pragma mark - keyboardHight -(void)viewWillAppear:(BOOL)animated { [self registerForKeyboardNotifications]; } -(void)viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)registerForKeyboardNotifications { //使用NSNotificationCenter 鍵盤出現時 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShown:) name:UIKeyboardWillChangeFrameNotification object:nil]; //使用NSNotificationCenter 鍵盤隐藏時 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; } //实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置 - (void)keyboardWillShown:(NSNotification*)aNotification {NSDictionary *info = [notification userInfo];
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
//输入框位置动画加载[UIView animateWithDuration:duration animations:^{
//do something
}];
}
//当键盘隐藏的时候- (void)keyboardWillBeHidden:(NSNotification*)aNotification{ //do something}