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

时间:2014-05-10 23:58:24   收藏:0   阅读:570

 

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

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!