代碼如下:
- (void)viewDidLoad
{
[super viewDidLoad];
//增加監(jiān)聽(tīng)例朱,當(dāng)鍵盤(pán)出現(xiàn)或改變時(shí)收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加監(jiān)聽(tīng)徙缴,當(dāng)鍵退出時(shí)收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
//當(dāng)鍵盤(pán)出現(xiàn)或改變時(shí)調(diào)用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//獲取鍵盤(pán)的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
}
//當(dāng)鍵退出時(shí)調(diào)用
- (void)keyboardWillHide:(NSNotification *)aNotification{}