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

iOS键盘高度的获取

作者: liu强 | 来源:发表于2017-06-19 11:57 被阅读0次

    <pre>

    • (void)registerNotification
      {
      [super registerNotification];

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
      }

    • (void)deregisterNotification
      {
      [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
      [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
      [super deregisterNotification];
      }

    • (void)handleKeyboardWillShowNotification:(NSNotification)noti
      {
      CGFloat tmpKeyboardVisibleHeight = 0;
      CGRect endRect = [(NSValue
      )noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
      tmpKeyboardVisibleHeight = self.view.height - endRect.origin.y;
      if (tmpKeyboardVisibleHeight != currentKeyboardVisibleHeight)
      {
      currentKeyboardVisibleHeight = tmpKeyboardVisibleHeight;
      [self scollContentContainer:currentKeyboardVisibleHeight];
      }
      }

    • (void)handleKeyboardWillChangeFrameNotification:(NSNotification)noti
      {
      //如果currentKeyboardVisibleHeight为0则代表键盘将会被隐藏,否则则会被显示出来
      CGRect endRect = [(NSValue
      )noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
      //不能判断beginRect和endRect是否相等,如果在不相等的时候才做�更新,程序会出现bug
      CGFloat tmpKeyboardVisibleHeight = 0;
      tmpKeyboardVisibleHeight = self.view.height - endRect.origin.y;
      if (tmpKeyboardVisibleHeight != currentKeyboardVisibleHeight)
      {
      currentKeyboardVisibleHeight = tmpKeyboardVisibleHeight;
      //如果密码框可以切换输入法,这儿可能会有潜在的bug
      [self scollContentContainer:currentKeyboardVisibleHeight];
      }
      }
      </pre>

    相关文章

      网友评论

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

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