iOS5键盘弹出view自适应

来源:互联网 发布:万硕网络 编辑:程序博客网 时间:2024/05/21 17:27
Java代码  收藏代码
  1. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];  
  2.     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];  
  3.       
  4. #ifdef __IPHONE_5_0  
  5.     float version = [[[UIDevice currentDevice] systemVersion] floatValue];  
  6.     if (version >= 5.0) {  
  7.         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];  
  8.     }  
  9. #endif  



Java代码  收藏代码
  1. #pragma -  
  2. #pragma keyboard notification  
  3. - (void)keyboardWillShow:(NSNotification*)notification {  
  4.     NSDictionary *userInfo = [notification userInfo];  
  5.       
  6.     NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];  
  7.       
  8.     CGRect keyboardRect = [aValue CGRectValue];  
  9.       
  10.     NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];  
  11.     NSTimeInterval animationDuration;  
  12.     [animationDurationValue getValue:&animationDuration];  
  13.       
  14.     [UIView beginAnimations:nil context:nil];  
  15.     [UIView setAnimationDuration:animationDuration];  
  16.       
  17.     ... do something ...  
  18.       
  19.     [UIView commitAnimations];  
  20. }  
  21.   
  22. - (void)keyboardWillHide:(NSNotification*)notification {  
  23.     NSDictionary *userInfo = [notification userInfo];  
  24.       
  25.     NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];  
  26.       
  27.     CGRect keyboardRect = [aValue CGRectValue];  
  28.       
  29.     NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];  
  30.     NSTimeInterval animationDuration;  
  31.     [animationDurationValue getValue:&animationDuration];  
  32.   
  33.     [UIView beginAnimations:nil context:nil];  
  34.     [UIView setAnimationDuration:animationDuration];  
  35.       
  36.     ... do something ...  
  37.       
  38.     [UIView commitAnimations];  
  39. }  


改变键盘大小时也会更新视图
原创粉丝点击