ios动态获得键盘高度,并改变对话框的位置

 

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动态获得键盘高度,并改变对话框的位置,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。