您可以使用UIKeyboardFrameBeginUserInfoKey和UIKeyboardFrameEndUserInfoKeyuserInfo
从字典中获取键盘大小。
这两个键返回一个NSValue
包含 a 的实例,该实例CGRect
在键盘的显示/隐藏动画的起点和终点处保存键盘的位置和大小。
编辑:
澄清一下,userInfo
字典来自一个NSNotification实例。它被传递给您向观察者注册的方法。例如,
- (void)someMethodWhereYouSetUpYourObserver
{
// This could be in an init method.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myNotificationMethod:)
name:UIKeyboardDidShowNotification
object:nil];
}
- (void)myNotificationMethod:(NSNotification*)notification
{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
}
编辑2:
另外,请不要忘记在您的dealloc
方法中删除自己作为观察者!这是为了避免在通知中心在释放对象后尝试通知您的对象时发生崩溃。