美文网首页
iOS监听键盘事件

iOS监听键盘事件

作者: methodname | 来源:发表于2016-11-25 12:59 被阅读0次

1.先设置监听者以及监听对象和事件

<pre>
<code class='objectivec hljs'>
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

2.实现监听方法

  • (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    // 获取键盘的frame
    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 获取键盘的动画时间
    NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    // do something
    }

  • (void)keyboardWillHide:(NSNotification *)notification {
    // do something
    }
    </code></pre>

相关文章

网友评论

      本文标题:iOS监听键盘事件

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