美文网首页
iOS获取键盘高度

iOS获取键盘高度

作者: 木子加白水 | 来源:发表于2018-06-02 18: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;
         
        }
    }
    
    

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

    相关文章

      网友评论

          本文标题:iOS获取键盘高度

          本文链接:https://www.haomeiwen.com/subject/vuwrsftx.html