點擊屏幕回收鍵盤是很簡單的,但是在scrollView上點擊回收鍵盤,直接調用那個方法就不能實現了
// 我的實現是這樣的
// 首先實現一個繼承自UIScrollView的Category,.m文件的實現
#import "UIScrollView+UITouch.h"
@implementation UIScrollView (UITouch)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesMoved:touches withEvent:event];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesEnded:touches withEvent:event];
[super touchesEnded:touches withEvent:event];
}
// 然后在要回收鍵盤的界面,導入這個類
#import "UIScrollView+UITouch.h"
// 在touchesBegan方法里,得到要釋放的textField,調用resignFirstResponder方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
MKInputPhoneNumberCell *inputPhoneNumberCell = (MKInputPhoneNumberCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[inputPhoneNumberCell.inputPhoneNumberTF resignFirstResponder];
MKPhoneCertifyTableViewCell *phoneCertifyCell = (MKPhoneCertifyTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
[phoneCertifyCell.inputCertifyTF resignFirstResponder];
}
@end
當鍵盤彈出時,有可能會遮蓋住輸入框,之前我采用把View放到scrollView上來處理,但是后來發現,讓View跟著鍵盤動起來效果更好
// 首先注冊通知,彈出鍵盤和鍵盤回收兩個
// 彈出鍵盤時view向上偏移,避免遮蓋輸入框
[[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 notification -
- (void)keyboardWillShow:(NSNotification *)note {
NSDictionary *info = [note userInfo];
// keyboardHeight 為鍵盤高度
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
[self animateViewWithKeyboardHeight: keyboardSize.height];
}
- (void)keyboardWillHide:(NSNotification *)note {
[self animateViewWithKeyboardHeight: 0.0];
}
- (void)animateViewWithKeyboardHeight:(CGFloat)keyboardHeight
{
NSTimeInterval animationDuration = 0.3f;
CGFloat width = self.bounds.size.width;
CGFloat height = self.bounds.size.height;
// 保持鍵盤和輸入框底部view至少有-10的間距,這里的間距自己來定
CGFloat space = -10;
CGFloat topSize = 0.0;
CGFloat viewH = _bottomBackView.frame.size.height + _bottomBackView.frame.origin.y + space; // 得到view底部的y
CGFloat deviceH = self.bounds.size.height;
// 屏幕高度 - (view底部的y + 鍵盤的高度), 如果>=0,則說明距離足夠,設置view.origin.y = 0;否則則說明view需要上移
CGFloat animateH = deviceH - viewH - keyboardHeight;
if (animateH >= 0) {
topSize = 0;
CGRect toRect = CGRectMake(0, topSize, width, height);
self.frame = toRect;
} else {
topSize = animateH;
CGRect toRect = CGRectMake(0, topSize, width, height);
[UIView animateWithDuration:animationDuration animations:^{
self.frame = toRect;
}];
}
}
// 最后注銷通知
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}