iOS获取键盘高度

2018-06-02  本文已影响888人  木子加白水

这是个老生常谈的问题,但是之前监听键盘通知总会遇到各种问题。这里记录下自己目前使用没出现问题的解决办法。直接上代码

#pragma mark - 键盘通知
- (void)addNoticeForKeyboard {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
}

//键盘farme值
- (void)keyboardFrameChange:(NSNotification *)notifi{
    
    NSDictionary *userInfo = notifi.userInfo;
    CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect beginFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    void(^animations)(void) = ^{
        [self willShowKeyboardFromFrame:beginFrame toFrame:endFrame];
    };
    
    void(^completion)(BOOL) = ^(BOOL finished){
        
    };
    
    [UIView animateWithDuration:duration delay:0.0f options:(curve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:animations completion:completion];
    
}

- (void)willShowKeyboardFromFrame:(CGRect)beginFrame toFrame:(CGRect)toFrame
{
    if (toFrame.origin.y  == [[UIScreen mainScreen] bounds].size.height){//键盘收起
 
    }else{ //键盘升起
        keyboardHeight = toFrame.size.height;
     
    }
}

谢谢。最近真是忙啊,心好累!

上一篇下一篇

猜你喜欢

热点阅读