(网上摘抄并进行总结)
首先说明下键盘显示消息和隐藏消息分别为:UIKeyboardWillShowNotification、UIKeyboardWillHideNotification;这个两个消息系统会自动发送,前提是自己注册接收此消息。
1、注册键盘显示消息通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
2、注册键盘隐藏消息通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
3、键盘显示的时候处理函数:
-(void)keyboardWillShow:(NSNotification*)notif{
NSDictionary* userInfo=[notif userInfo];/*个人理解为解析消息内容*/
NSValue* avalue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];/*个人理解为从字典中获取UIKeyboardFrameEndUserInfoKey对应的键值*/
CGRect keyboardRect=[avalue CGRectValue];/*个人理解为将获取到的键值转换为需要的坐标类型*/
int height=keyboardRect.size.height;//获取键盘高度
/*根据获取到的键盘高度,调节界面内容高度*/
}
4、键盘隐藏的时候处理函数:
-(void)keyboardWillHide:(NSNotification*)notif{
/*键盘隐藏事件一般很少用到,至少我现在还不需要这个消息,先留着,以备后期使用*/
}
5、移除注册的键盘显示和隐藏消息(放在dealloc中,软件退出系统会自动调用):
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}