NSNotificationCenter:键盘出现、消失时的通知
UIKeyboardWillShowNotification;UIKeyboardDidShowNotification;UIKeyboardWillHideNotification;UIKeyboardDidHideNotification;
在要使用键盘的视图控制器中(既viewDidLoad中),接收键盘事件的通知:
- (void) registerForKeyboardNotifications
{
//键盘改变时候会调用
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(keyboardWasShown:)
name:UIKeyboardWillChangeFrameNotification object:nil];
//键盘收起会调用
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification object:nil];
}
- (void) keyboardWasShown:(NSNotification *) notif
{
NSDictionary *info = [notif userInfo];
NSValue *value = [info
objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [value
CGRectValue].size;
_nameField.frame=CGRectMake(5,
self.view.frame.size.height-keyboardSize.height-30, 155,
30);
_sexField.frame=CGRectMake(160,
self.view.frame.size.height-keyboardSize.height-30, 155, 30);
}
- (void) keyboardWasHidden:(NSNotification *)
notif
{
NSDictionary *info = [notif
userInfo];
NSValue *value = [info
objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [value
CGRectValue].size;
NSLog(@"keyboardWasHidden keyBoard:%f", keyboardSize.height);
// keyboardWasShown = NO;
_nameField.frame=CGRectMake(5, 269, 155, 30);
_sexField.frame=CGRectMake(160, 269, 155, 30);
}
ios动态获得键盘高度,并改变对话框的位置,布布扣,bubuko.com