1.键盘的消失方式
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self.view endEditing:YES];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self.view endEditing:YES];
}
2.注册键盘监听
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
- (void) keyboardFrameChange:(NSNotification *)notice {
CGFloat time=[notice.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat endY=[notice.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
CGFloat offset=endY-self.view.bounds.size.height;
[UIView animateWithDuration:time animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, offset);
}];
}
- (void) dealloc {
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
[center removeObserver:self];
}
3.文本框的代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}