要监听键盘的弹起和回收,系统没有提供api,但我们可以通过监听事件来实现:
弹起:[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
回收:[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
然后自己实现监听到响应事件之后的操作:
- (void) keyboardWillShow : (NSNotification*)notification {
CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; //获得键盘的rect
//通过rect做响应的弹起等
} keyboardWillHide也一样
最后在dealloc中删除观察者