首页 > 技术文章 > uiviewcontroller 键盘不遮挡信息

Clin 2013-07-18 21:32 原文

//添加监听事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

//监听回调
#pragma mark - keyboard 
- (void)keyboardWillShow:(NSNotification*)notification {

    CGRect frame = self.view.frame;
    frame.origin.y -= 166;
    //frame.size.height +=216;
    // Shrink view's inset by the keyboard's height, and scroll to show the text field/view being edited
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
    self.view.frame = frame;
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification*)notification {

    CGRect frame = self.view.frame;
    frame.origin.y += 166;
    //frame.size.height -=216;
    //self.view移回原位置
    // Restore dimensions to prior size
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
    self.view.frame = frame;
    [UIView commitAnimations];
}

 

推荐阅读