每次用到键盘通知都从老代码里找,太麻烦了,总结一下:
首先在页面启动的时候加载通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
再在dealloc时移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
键盘通知的接收方法:
#pragma mark - UIKeyboardNotification
- (void)keyboardWillShow:(NSNotification *)noti{
CGFloat keyboardHeight = [[[noti userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
}
- (void)keyboardWillHide:(NSNotification *)noti{
CGFloat keyboardHeight = 0;
}
网友评论