码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-05-10 23:58:24      阅读:570      评论:0      收藏:0      [点我收藏+]

标签:style   color   c   strong   a   使用   

 

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

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

标签:style   color   c   strong   a   使用   

原文地址:http://www.cnblogs.com/hewenpeng/p/3721069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!