今是昨非

今是昨非

日出江花红胜火,春来江水绿如蓝

iOS Development Knowledge Point 3 - Keyboard

title: iOS Development Knowledge Point 3 - Keyboard
date: 2015-11-17 22:08
tags: Technology#

iOS Development Knowledge Point 3 - Keyboard#

It is simple to dismiss the keyboard by tapping on the screen, but when tapping on a scrollView, directly calling that method will not work.

// This is my implementation
// First, implement a Category that inherits from UIScrollView in a .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 put the View on the scrollView to handle this, but later I found that it is better to make the View move with the keyboard.

// First, register two notifications, one for the keyboard popping up and the other for the keyboard being dismissed
// 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, the spacing can be adjusted
    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 there is enough distance, 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];
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.