IOS8.0之后可以安装第三方键盘,如搜狗输入法之类的。
获得的高度都为0.这是因为键盘弹出的方法:- (void)keyBoardWillShow:(NSNotification *)notification需要执行三次,你如果打印一下,你会发现键盘高度为:第一次:0;第二次:216:第三次:282.并不是获取不到高度,而是第三次才获取真正的高度.
可以在UIKeyboardDidChangeFrameNotification的通知中实现,这里需要注意的是:在弹出键盘时该方法执行3次,需要进行处理,已达到所要的效果.
注册键盘事件:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardChange:) name:UIKeyboardDidChangeFrameNotification object:nil];
pragma mark–键盘改变事件的触发
(void)keyBoardChange:(NSNotification *)notification{
NSDictionary *dict = notification.userInfo;
NSValue *aValue = [dict objectForKey:UIKeyboardFrameEndUserInfoKey];
NSNumber *animationTime = [dict objectForKey:@”UIKeyboardAnimationDurationUserInfoKey”];CGRect keyboardRect = [aValue CGRectValue];
CGFloat keyHeight = (HEIGHT(self.view)-Y(searBar)-HEIGHT(searBar))-keyboardRect.size.height;
if(keyHeight<=0){
[UIView animateWithDuration:[animationTime doubleValue] animations:^{
self.view.frame =CGRectMake(0, keyHeight, WIDTH(self.view), HEIGHT(self.view));
} completion:^(BOOL finished) {
}];
}
}
pragma mark–键盘隐藏事件
- (void)keyBoardDidHide:(NSNotification *)notification{
self.view.frame = CGRectMake(0, 0, WIDTH(self.view), HEIGHT(self.view));
}
或是:
pragma mark–键盘将要弹出
(void)keyBoardWillShow:(NSNotification *)notification{
NSDictionary *dict = notification.userInfo;
NSValue *aValue = [dict objectForKey:UIKeyboardFrameEndUserInfoKey];
// NSNumber *animationTime = [dict objectForKey:@”UIKeyboardAnimationDurationUserInfoKey”];CGRect keyboardRect = [aValue CGRectValue];
CGFloat keyHeight = (HEIGHT(self.view)-Y(searBar)-HEIGHT(searBar)-5*screenHeight/568)-keyboardRect.size.height;
if(keyHeight<=0){
[UIView animateWithDuration:0.0f animations:^{
self.view.frame =CGRectMake(0, keyHeight, WIDTH(self.view), HEIGHT(self.view));
} completion:^(BOOL finished) {
}];
}
}
pragma mark–键盘将要隐藏
(void)keyBoardWillHide:(NSNotification *)notification{
self.view.frame = CGRectMake(0, 0, WIDTH(self.view), HEIGHT(self.view));
}