监听键盘- KeyBoard
- (void)KeyBoardShowOrHide
{
// NSNotificationCenter 通知中心
// 检测键盘将要消失的状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard:) name:UIKeyboardWillHideNotification object:nil];
// 检测键盘将要弹出的状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyBoard:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)hideKeyBoard:(NSNotification *)sender
{
NSLog(@"键盘退出");
// 还原
self.view.transform = CGAffineTransformIdentity;
}
- (void)showKeyBoard:(NSNotification *)sender
{
//NSLog(@"%@",sender.userInfo);
// 获取键盘高度
CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyBoardH = rect.size.height;
self.view.transform = CGAffineTransformMakeTranslation(0, - keyBoardH);
}
注意:当写注册页面时,由于textField太多,用此方法会导致,第一个textField在编辑时看不见,看得见时,有没有键盘。根据李老师的提议,把textField添加到scrollView中,不过我暂时没有实现过。