美文网首页
监听键盘弹出 iOS

监听键盘弹出 iOS

作者: 想飞的菜鸟 | 来源:发表于2018-03-24 14:33 被阅读0次

    //增加监听,当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(keyboardWillShow:)
    name:UIKeyboardWillShowNotification
    object:nil];
    //增加监听,当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(keyboardWillHide:)
    name:UIKeyboardWillHideNotification
    object:nil];

    //当键盘出现或改变时调用

    • (void)keyboardWillShow:(NSNotification *)aNotification {
      //获取键盘的高度
      NSDictionary *userInfo = [aNotification userInfo];
      NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
      CGRect keyboardRect = [aValue CGRectValue];
      int height = keyboardRect.size.height;
      // 需要进行的操作
      }
      //当键退出时调用
    • (void)keyboardWillHide:(NSNotification *)aNotification {
      // 界面复原
      }

    相关文章

      网友评论

          本文标题:监听键盘弹出 iOS

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