UIKeyboardWillShowNotification//弹出
UIKeyboardWillHideNotification//收起
所以需要监听这两个 最一些操作 比如 视图的移动
//注册观察键盘的变化
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didClickKeyboard:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didKboardDisappear:) name:UIKeyboardWillHideNotification object:nil];
方法实现
一般的:
#pragma mark - 键盘即将跳出
-(void)didClickKeyboard:(NSNotification *)sender{
CGFloat durition = [sender.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
CGRect keyboardRect = [sender.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
CGFloat keyboardHeight = keyboardRect.size.height;
[UIView animateWithDuration:durition animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight);
}];
}
#pragma mark - 当键盘即将消失
-(void)didKboardDisappear:(NSNotification *)sender{
CGFloat duration = [sender.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
[UIView animateWithDuration:duration animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}
如果视图在scrollview上边:
#pragma mark - 键盘即将跳出
-(void)didClickKeyboard:(NSNotification *)sender{
[UIView animateWithDuration:0.1 animations:^{
[self.scrollView setContentOffset:CGPointMake(0,30) animated:YES];
}];
}
#pragma mark - 当键盘即将消失
-(void)didKboardDisappear:(NSNotification *)sender{
[UIView animateWithDuration:0.1 animations:^{
[self.scrollView setContentOffset:CGPointMake(0,0) animated:YES];
}];
}