iOS Development Knowledge Point 3 - Keyboard#
It is simple to dismiss the keyboard by tapping on the screen, but when tapping on a scrollView to dismiss the keyboard, directly calling that method does not work.
// This is my implementation
// First, implement a Category that inherits from UIScrollView, in the .m file
#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];
}
// Then, import this class in the view where the keyboard needs to be dismissed
#import "UIScrollView+UITouch.h"
// In the touchesBegan method, get the textField to be released and call the resignFirstResponder method
- (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
When the keyboard pops up, it may cover the input box. Previously, I used to place the View on the scrollView to handle it, but later I found that it works better to make the View move with the keyboard.
// First, register two notifications, keyboardWillShow and keyboardWillHide
// When the keyboard pops up, the view is shifted upwards to avoid covering the input box
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
// Then implement the notification methods
#pragma mark - keyboard notification -
- (void)keyboardWillShow:(NSNotification *)note {
NSDictionary *info = [note userInfo];
// keyboardHeight is the height of the keyboard
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;
// Keep at least a -10 spacing between the keyboard and the bottom view, you can define the spacing here
CGFloat space = -10;
CGFloat topSize = 0.0;
CGFloat viewH = _bottomBackView.frame.size.height + _bottomBackView.frame.origin.y + space; // Get the y-coordinate of the bottom of the view
CGFloat deviceH = self.bounds.size.height;
// Screen height - (y-coordinate of the bottom of the view + height of the keyboard), if >= 0, it means the distance is enough, set view.origin.y = 0; otherwise, it means the view needs to move up
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;
}];
}
}
// Finally, unregister the notifications
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}