#pragma -mark -keyBoardNotification
- (void)addNotification {
// if(IOS_VERSION<5.0)
// {
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillHideNotification object:nil];
// }else{
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// }
if (IOS_VERSION>5.0) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
}
- (void)removeNotification
{
if (IOS_VERSION>5.0) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
}
- (void)keyboardWillShow:(id)notification
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2
NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
#else
NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];
#endif
CGRect keyboardBounds;
[keyboardBoundsValue getValue:&keyboardBounds];
//键盘高度 216 汉字键盘:252
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
//处理移动事件,将各视图设置最终要达到的状态
// NSInteger offset = self.view.frame.size.height-keyboardBounds.origin.y;
// NSInteger offset = keyboardBounds.origin.y;
// CGRect listFrame = CGRectMake(0, -offset, self.view.frame.size.width,self.view.frame.size.height);
// NSLog(@"keybodarH : %f", keyboardBounds.size.height);
if([_textFiled isFirstResponder]) {
// CGFloat lastY = downImageView.frame.origin.y +downImageView.frame.size.height;
// CGFloat nowY = backgroundView.frame.size.height-keyboardBounds.size.height;
// CGFloat offset = lastY-nowY;
// NSLog(@"lastY:%f nowY:%f offset:%f", lastY, nowY,offset);
// if(offset>0) {
// backgroundView.frame = CGRectMake(0, 44-offset, 320, 460);
// }
if (keyboardBounds.size.height == 216) {
backgroundView.frame = CGRectMake(0, 4, 320, 460);
} else {
backgroundView.frame = CGRectMake(0, 4-36, 320, 460);
}
}
// 239.000000 nowY:200.000000 offset:39.000000 1
[UIView commitAnimations];
}
}
以前的老代码,随便改了改
监听键盘事件。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
此通知在以下两种情况都会调用
1:键盘显示
2:当前为英文输入,点击切换为中文输入,(5.0以后,中文输入,键盘上面多出一栏来) 都会调用
英文输入时候键盘高度:216
中文输入时候键盘高度:252
这代码,真它丫乱。 (T_T)