- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
// 键盘显示
- (void)keyboardWillShow:(NSNotification *)notification
{
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat height = keyboardFrame.origin.y;
// 计算视图需要移动的距离
CGFloat space = self.imageView.frame.origin.y + self.imageView.frame.size.height;
// 得出键盘距离输入框的间距
CGFloat trsformY = height - space;
if (trsformY < 0) {
CGRect frame = self.view.frame;
frame.origin.y = trsformY;
self.view.frame = frame;
}
}
// 键盘隐藏
- (void)keyboardWillHide:(NSNotification *)notification
{
CGRect frame = self.view.frame;
frame.origin.y = 64;
self.view.frame = frame;
}