在群里看到有人问:ios如何判断键盘已经显示在界面上。
其实这个解决很简单:
写一个单例来管理键盘的状态。
这个单例在初始化方法init种监听2个事件,分别是
UIKeyboardDidShowNotification(键盘弹出通知)和
UIKeyboardWillHideNotification (键盘消失通知 然后在相应的方法中设置一个属性就行了。
大致的实现如下:
-(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];
_keyboardIsVisible = NO;
}
return self;
}
- (void)keyboardDidShow
{
_keyboardIsVisible = YES;
}
- (void)keyboardDidHide
{
_keyboardIsVisible = NO;
}
- (BOOL)keyboardIsVisible
{
return _keyboardIsVisible;
}