iOS8以后第三方键盘,获取高度为0的问题

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));

}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值