[ios] 适应键盘高度变化

    1. - (void)regNotification  
    2. {  
    3.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];  
    4. }  
    5.   
    6. - (void)unregNotification  
    7. {  
    8.     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];  
    9. }  
    10.   
    11. #pragma mark - notification handler  
    12.   
    13. - (void)keyboardWillChangeFrame:(NSNotification *)notification  
    14. {  
    15.     NSDictionary *info = [notification userInfo];  
    16.     CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];  
    17.     CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];  
    18.     CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];  
    19.       
    20.     CGFloat yOffset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y;  
    21.       
    22.     CGRect inputFieldRect = self.inputTextField.frame;  
    23.     CGRect moreBtnRect = self.moreInputTypeBtn.frame;  
    24.       
    25.     inputFieldRect.origin.y += yOffset;  
    26.     moreBtnRect.origin.y += yOffset;  
    27.       
    28.     [UIView animateWithDuration:duration animations:^{  
    29.         self.inputTextField.frame = inputFieldRect;  
    30.         self.moreInputTypeBtn.frame = moreBtnRect;  
    31.     }];  

posted @ 2013-04-26 04:39  金建彤  阅读(177)  评论(0编辑  收藏  举报