preface
Recently, I participated in the development of a recruitment app, and the registration process is quite large. Basically, it is a form list style with cell input box, and the unavoidable problem of blocking the keyboard will be encountered. I believe you must have encountered similar problems, today here to share with you, the solution to this problem.
Implementation scheme
First, the form is a list (UITableView or UICollectionView), as shown in Figure 1. When the user clicks on input boxes 1, 2, 3, 4, 5, the keyboard pops up but is not blocked. In this case, there is no need to do anything. When the user clicks on input boxes 6, 7, and 8, the keyboard pops up and blocks the input box. To make the input box appear, we can either change the frame Y value of the entire list view up or change the Y value of the contentoffset on the list. Let’s choose the second way to fulfill our requirements.
Specific implementation is divided into the following steps:
- Listen for keyboard up and down events
- Computing keyboard height
- Calculate the difference between the y values of contentoffset to change and modify the values of contentoffset
- Fold up the keyboard while swiping through the list
- Restores the value of contentoffset when the keyboard is folded up
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardAction:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardAction:) name:UIKeyboardWillHideNotification object:nil];
Copy the code
Step 2 __ calculate the height
- (void)keyboardAction:(NSNotification*)sender{NSDictionary *useInfo = [sender userInfo]; NSValue *value = [useInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; // Keyboard height CGFloat height = [value CGRectValue].size. Height;if([sender name isEqualToString: UIKeyboardWillShowNotification]) {/ / keyboard pop-up}else{// when the keyboard is down}}Copy the code
Step 3 __ Calculate the difference between the y values of contentoffset and modify the values of contentoffset. Let’s take the example of clicking on the 7th input box. When clicking on the 7th input box, we want the 7th input box to come up to the keyboard, as shown in Figure 2
Delta =3-(2-1), 2 is the height of the list, 1 is the height of the keyboard, and 3 is the maxY value of the cell where the 7th input box is located – the Y value of the current list of contentoffset, as shown in Figure 3.
1 and 2 are easy to get. The key is to get the value of 3. We just need to get the instance of the cell where the 7th input box is, and then we can get this value by using CGRectGetMaxY(Cell.frame). Below is the code to get the cell instance
- (UICollectionViewCell *)firstResponderCell { __block UICollectionViewCell *cell = nil; [self.collectionView.visibleCells enumerateObjectsUsingBlock:^(__kindof UICollectionViewCell * _Nonnull obj, NSUInteger IDX, BOOL * _Nonnull stop) {UICollectionViewCell *visibleCell = objif(visibleCell.textField.isFirstResponder) { cell = visibleCell; }}];return cell;
}
Copy the code
Calculate difference changes contentoffset
- (void)keyboardAction:(NSNotification*)sender{NSDictionary *useInfo = [sender userInfo]; NSValue *value = [useInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; // keyboardHeight CGFloat keyboardHeight = [value CGRectValue].size. Height; / / list of highly CGFloat collectionViewHeight = self. CollectionView. Frame. The size, height;if([sender name isEqualToString: UIKeyboardWillShowNotification]) {/ / keyboard pops up / / input box focus for cell UICollectionViewCell * cell = [self firstResponderCell];if(cell) {/ / cell maxY value CGFloat cellMaxY = CGRectGetMaxY (cell. The frame) - self. CollectionView. ContentOffset. Y; // The difference is 3 - (2-1).if(cellMaxY > collectionViewHeight-keyboardHeight) { Self. delta = cellMaxY-(collectionViewHeight-keyboardHeight); self.collectionView.contentOffset = CGPointMake(0, self.collectionView.contentOffset.y+self.delta); }}}else{// when the keyboard is down}}Copy the code
Step 4 __ Fold up the keyboard while swiping the list
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
UICollectionViewCell *cell = [self firstResponderCell];
if(cell) { [cell.textField resignFirstResponder]; }}Copy the code
Step 5 __ Restore the value of contentoffset when the keyboard is down
- (void)keyboardAction:(NSNotification*)sender{NSDictionary *useInfo = [sender userInfo]; NSValue *value = [useInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; // keyboardHeight CGFloat keyboardHeight = [value CGRectValue].size. Height; / / list of highly CGFloat collectionViewHeight = self. CollectionView. Frame. The size, height;if([sender name isEqualToString: UIKeyboardWillShowNotification]) {/ / keyboard emerged}else{/ / the keyboard up / / according to the self. The delta recover self. CollectionView. ContentOffset = CGPointMake (0, self.collectionView.contentOffset.y-self.delta); self.delta = 0 } }Copy the code
Realization finished, how, is very simple ~~~~~
The original source: www.jianshu.com/p/1ffaa7782…