判断键盘是否显示

有时我们需要判断键盘的显示状态,以便做后续的处理,下面的代码是写了一个单例进行判断

-(id)init{
    self = [super init];
    if (self)
    {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center  addObserver:self selector:@selector(keyboardDidShow)  name:UIKeyboardDidShowNotification  object:nil];
        [center addObserver:self selector:@selector(keyboardDidHide)  name:UIKeyboardWillHideNotification object:nil];
        _keyboardIsShow = NO;
    }
    return self;
}

- (void)keyboardDidShow
{
    _keyboardIsShow = YES;
}

- (void)keyboardDidHide
{
    _keyboardIsShow = NO;
}

- (BOOL)keyboardIsVisible
{
    return _keyboardIsShow;
}

猜你喜欢

转载自blog.csdn.net/qq_30963589/article/details/52036625