1.先設(shè)置監(jiān)聽(tīng)者以及監(jiān)聽(tīng)對(duì)象和事件
<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.實(shí)現(xiàn)監(jiān)聽(tīng)方法
(void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
// 獲取鍵盤(pán)的frame
CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 獲取鍵盤(pán)的動(dòng)畫(huà)時(shí)間
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// do something
}(void)keyboardWillHide:(NSNotification *)notification {
// do something
}
</code></pre>