IOS solves the problem that forms are covered by the keyboard, while ios forms and keyboards are covered
Problem
When processing a form, you will surely encounter the problem that the input control is covered by the keyboard ,:
Instance
The left side is a normal form, the middle side is a 2B form, and the right side is a literary form.
Analysis
There are two steps to solve this problem:
The code is written in this step.
How can we implement these steps Step by Step?
Capture Keyboard Events
Capture Keyboard Events
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardShow:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardHide:) name:UIKeyboardWillHideNotification object:nil];
- (void)actionKeyboardShow:(NSNotification *)notification { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardShow:) name:UIKeyboardDidChangeFrameNotification object:nil]; }
- (void)actionKeyboardHide:(NSNotification *)notification { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardShow:) name:UIKeyboardDidShowNotification object:nil]; }
|
Calculate and adjust the keyboard height
UITableView
Of
frame
Calculate the keyboard height and adjust the frame of UITableView
1 2 3 4 5 6 7 8 9 10 11 12 13
|
- (void)actionKeyboardShow:(NSNotification *)notification { CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size self.tableView.frame = CGRectMake(0, 0, 320, self.view.h-keyboardSize.height); [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardShow:) name:UIKeyboardDidChangeFrameNotification object:nil]; }
|
Obtain the control currently being input
Here, we have to say that common programmers generally obtain this information.
Category of UIView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
- (UIView *) getFirstResponder { if (self.isFirstResponder) { return self; } for (UIView *subView in self.subviews) { UIView *firstResponder = [subView getFirstResponder]; if (firstResponder != nil) { return firstResponder; } } return nil; }
|
Although it is true, it should be obtained by art programmers like this.
UIResponder Category
1 2 3 4 5 6 7 8 9 10 11
|
static __weak id currentFirstResponder;
+(id)currentFirstResponder { currentFirstResponder = nil; [[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:) to:nil from:nil forEvent:nil]; return currentFirstResponder; }
-(void)findFirstResponder:(id)sender { currentFirstResponder = self; }
|
Likewise, there are three options for removing the keyboard.
1 2 3 4 5
|
[someView resignFirstResponder];
[self.view endEditing:YES];
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
|
How to choose? It's up to U.
Calculate
UITableView
And scroll to its position to make it visible.
Calculate its position in UITableView and scroll to its position to make it visible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
- (void)actionKeyboardShow:(NSNotification *)notification { CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; self.tableView.frame = CGRectMake(0, 0, 320, self.view.h-keyboardSize.height); UIView *v = [UIResponder currentFirstResponder]; if ( v ) { while ( ![v isKindOfClass:[UITableViewCell class]]) { v = v.superview; } UITableViewCell *cell = (UITableViewCell*)v; [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForRowAtPoint:cell.center] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; } [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardShow:) name:UIKeyboardDidChangeFrameNotification object:nil]; } |