现在的位置: 首页 > 综合 > 正文

ios 如何判断键盘是否已经显示

2017年08月14日 ⁄ 综合 ⁄ 共 802字 ⁄ 字号 评论关闭

在群里看到有人问: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;
}

转载自:http://blog.sina.com.cn/s/blog_6531b9b80101c20u.html

参考:http://www.cnblogs.com/xinus/archive/2013/01/22/ios-keybord-notification.html

抱歉!评论已关闭.