From 73af6fae610a6657228e33888211104ea59aa0d1 Mon Sep 17 00:00:00 2001 From: Dasmer Singh Date: Thu, 10 Apr 2014 11:05:19 -0400 Subject: [PATCH 1/3] Made improvements so keyboard controls work during scrolling --- BZGFormViewController.podspec | 4 +- BZGFormViewController/BZGFormViewController.m | 87 +++++++++++++------ 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/BZGFormViewController.podspec b/BZGFormViewController.podspec index 5964999..7755bf4 100644 --- a/BZGFormViewController.podspec +++ b/BZGFormViewController.podspec @@ -1,13 +1,13 @@ Pod::Spec.new do |s| s.name = 'BZGFormViewController' - s.version = '2.2.0' + s.version = '2.3.1' s.license = 'MIT' s.summary = 'A library for creating dynamic forms.' s.homepage = 'https://github.com/benzguo/BZGFormViewController' s.author = { 'Ben Guo' => 'benzguo@gmail.com' } s.source = { :git => 'https://github.com/benzguo/BZGFormViewController.git', - :tag => 'v2.2.0' + :tag => "v#{s.version}" } s.dependency 'ReactiveCocoa', '~>2.2.4' s.dependency 'libextobjc', '~>0.4' diff --git a/BZGFormViewController/BZGFormViewController.m b/BZGFormViewController/BZGFormViewController.m index bd26e49..bcb1fe4 100644 --- a/BZGFormViewController/BZGFormViewController.m +++ b/BZGFormViewController/BZGFormViewController.m @@ -15,6 +15,8 @@ @interface BZGFormViewController () @property (nonatomic, assign) UITableViewStyle style; @property (nonatomic, assign) BOOL isValid; @property (nonatomic, strong) BZGKeyboardControl *keyboardControl; +@property (nonatomic, copy) void (^newFirstResponder)(); + @end @@ -52,22 +54,22 @@ - (void)loadView self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [self.tableView setSeparatorInset:UIEdgeInsetsZero]; - } + } self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.backgroundColor = BZG_TABLEVIEW_BACKGROUND_COLOR; - + UIView *contentView = [[UIView alloc] initWithFrame:CGRectZero]; contentView.autoresizesSubviews = YES; contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [contentView addSubview:self.tableView]; - + self.view = contentView; - + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; - + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification @@ -96,12 +98,12 @@ - (BZGInfoCell *)infoCellBelowFormCell:(BZGTextFieldCell *)cell NSUInteger cellIndex = [self.formCells indexOfObject:cell]; if (cellIndex == NSNotFound) return nil; if (cellIndex + 1 >= self.formCells.count) return nil; - + UITableViewCell *cellBelow = self.formCells[cellIndex + 1]; if ([cellBelow isKindOfClass:[BZGInfoCell class]]) { return (BZGInfoCell *)cellBelow; } - + return nil; } @@ -112,11 +114,11 @@ - (void)showInfoCellBelowFormCell:(BZGTextFieldCell *)cell NSIndexPath *indexPath = [NSIndexPath indexPathForRow:cellIndex+1 inSection:self.formSection]; - + // if an info cell is already showing, do nothing BZGInfoCell *infoCell = [self infoCellBelowFormCell:cell]; if (infoCell) return; - + // otherwise, add the cell's info cell to the table view [self.formCells insertObject:cell.infoCell atIndex:cellIndex+1]; [self.tableView insertRowsAtIndexPaths:@[indexPath] @@ -127,11 +129,11 @@ - (void)removeInfoCellBelowFormCell:(BZGTextFieldCell *)cell { NSUInteger cellIndex = [self.formCells indexOfObject:cell]; if (cellIndex == NSNotFound) return; - + // if no info cell is showing, do nothing BZGInfoCell *infoCell = [self infoCellBelowFormCell:cell]; if (!infoCell) return; - + // otherwise, remove it [self.formCells removeObjectAtIndex:cellIndex+1]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:cellIndex+1 @@ -144,10 +146,10 @@ - (void)updateInfoCellBelowFormCell:(BZGTextFieldCell *)cell if (!cell.textField.editing && (cell.validationState == BZGValidationStateInvalid || cell.validationState == BZGValidationStateWarning)) { - [self showInfoCellBelowFormCell:cell]; - } else { - [self removeInfoCellBelowFormCell:cell]; - } + [self showInfoCellBelowFormCell:cell]; + } else { + [self removeInfoCellBelowFormCell:cell]; + } } #pragma mark - Finding cells @@ -168,7 +170,7 @@ - (BZGTextFieldCell *)nextFormCell:(BZGTextFieldCell *)cell { NSUInteger cellIndex = [self.formCells indexOfObject:cell]; if (cellIndex == NSNotFound) return nil; - + for (NSUInteger i = cellIndex + 1; i < self.formCells.count; ++i) { UITableViewCell *cell = self.formCells[i]; if ([cell isKindOfClass:[BZGTextFieldCell class]]) { @@ -232,7 +234,7 @@ - (void)textFieldDidBeginEditing:(UITextField *)textField } NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; - [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; + [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; if (self.showsKeyboardControl) { [self accesorizeTextField:textField]; } @@ -245,12 +247,12 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang if (!cell) { return YES; } - + NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string]; if (cell.shouldChangeTextBlock) { shouldChange = cell.shouldChangeTextBlock(cell, newText); } - + [self updateInfoCellBelowFormCell:cell]; return shouldChange; } @@ -275,11 +277,11 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField if (!cell) { return YES; } - + if (cell.shouldReturnBlock) { shouldReturn = cell.shouldReturnBlock(cell, textField.text); } - + BZGTextFieldCell *nextCell = [self nextFormCell:cell]; if (!nextCell) { [cell.textField resignFirstResponder]; @@ -287,7 +289,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField else { [nextCell.textField becomeFirstResponder]; } - + [self updateInfoCellBelowFormCell:cell]; return shouldReturn; } @@ -300,8 +302,8 @@ - (void)formCell:(BZGFormCell *)formCell didChangeValidationState:(BZGValidation for (BZGFormCell *cell in self.formCells) { if ([cell isKindOfClass:[BZGFormCell class]]) { isValid = isValid && - (cell.validationState == BZGValidationStateValid || - cell.validationState == BZGValidationStateWarning); + (cell.validationState == BZGValidationStateValid || + cell.validationState == BZGValidationStateWarning); } } self.isValid = isValid; @@ -311,14 +313,14 @@ - (void)formCell:(BZGFormCell *)formCell didChangeValidationState:(BZGValidation - (void)keyboardWillShow:(NSNotification *)notification { CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; - + UIEdgeInsets contentInsets; if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) { contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height), 0.0); } else { contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.width), 0.0); } - + self.tableView.contentInset = contentInsets; self.tableView.scrollIndicatorInsets = contentInsets; } @@ -353,15 +355,44 @@ - (BZGKeyboardControl *)keyboardControl { } - (void)navigateToPreviousCell: (id)sender { - [self.keyboardControl.previousCell.textField becomeFirstResponder]; + BZGTextFieldCell *previousCell = self.keyboardControl.previousCell; + [self navigateToDestinationCell:previousCell]; } - (void)navigateToNextCell { - [self.keyboardControl.nextCell.textField becomeFirstResponder]; + BZGTextFieldCell *nextCell = self.keyboardControl.nextCell; + [self navigateToDestinationCell:nextCell]; +} + +- (void)navigateToDestinationCell:(BZGTextFieldCell *)destinationCell { + [self.keyboardControl.currentCell resignFirstResponder]; + if ([[self.tableView visibleCells] containsObject:destinationCell]) { + [destinationCell.textField becomeFirstResponder]; + } + else { + NSUInteger row = [self.formCells indexOfObject:destinationCell]; + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:self.formSection]; + self.newFirstResponder = ^{ + [destinationCell.textField becomeFirstResponder]; + }; + [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; + } } - (void)doneButtonPressed { [self.keyboardControl.currentCell.textField resignFirstResponder]; } +#pragma mark - UIScrollView Methods + +- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { + if (scrollView == self.tableView) { + if (self.newFirstResponder) { + self.newFirstResponder(); + self.newFirstResponder = nil; + } + } +} + @end + From 6078152483b84df95d49cccdba4a2ca466530844 Mon Sep 17 00:00:00 2001 From: Ben Guo Date: Thu, 10 Apr 2014 13:16:12 -0400 Subject: [PATCH 2/3] newFirstResponder -> didEndScrollingBlock --- BZGFormViewController/BZGFormViewController.m | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/BZGFormViewController/BZGFormViewController.m b/BZGFormViewController/BZGFormViewController.m index bcb1fe4..f7be739 100644 --- a/BZGFormViewController/BZGFormViewController.m +++ b/BZGFormViewController/BZGFormViewController.m @@ -15,8 +15,7 @@ @interface BZGFormViewController () @property (nonatomic, assign) UITableViewStyle style; @property (nonatomic, assign) BOOL isValid; @property (nonatomic, strong) BZGKeyboardControl *keyboardControl; -@property (nonatomic, copy) void (^newFirstResponder)(); - +@property (nonatomic, copy) void (^didEndScrollingBlock)(); @end @@ -372,7 +371,7 @@ - (void)navigateToDestinationCell:(BZGTextFieldCell *)destinationCell { else { NSUInteger row = [self.formCells indexOfObject:destinationCell]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:self.formSection]; - self.newFirstResponder = ^{ + self.didEndScrollingBlock = ^{ [destinationCell.textField becomeFirstResponder]; }; [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; @@ -387,9 +386,9 @@ - (void)doneButtonPressed { - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { if (scrollView == self.tableView) { - if (self.newFirstResponder) { - self.newFirstResponder(); - self.newFirstResponder = nil; + if (self.didEndScrollingBlock) { + self.didEndScrollingBlock(); + self.didEndScrollingBlock = nil; } } } From 60205fce8515b04c329bbe5d59e7c96246f15807 Mon Sep 17 00:00:00 2001 From: Ben Guo Date: Thu, 10 Apr 2014 13:18:16 -0400 Subject: [PATCH 3/3] Trim whitespace --- BZGFormViewController/BZGFormViewController.m | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/BZGFormViewController/BZGFormViewController.m b/BZGFormViewController/BZGFormViewController.m index f7be739..118dfd3 100644 --- a/BZGFormViewController/BZGFormViewController.m +++ b/BZGFormViewController/BZGFormViewController.m @@ -56,19 +56,19 @@ - (void)loadView } self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.backgroundColor = BZG_TABLEVIEW_BACKGROUND_COLOR; - + UIView *contentView = [[UIView alloc] initWithFrame:CGRectZero]; contentView.autoresizesSubviews = YES; contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [contentView addSubview:self.tableView]; - + self.view = contentView; - + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; - + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification @@ -97,12 +97,12 @@ - (BZGInfoCell *)infoCellBelowFormCell:(BZGTextFieldCell *)cell NSUInteger cellIndex = [self.formCells indexOfObject:cell]; if (cellIndex == NSNotFound) return nil; if (cellIndex + 1 >= self.formCells.count) return nil; - + UITableViewCell *cellBelow = self.formCells[cellIndex + 1]; if ([cellBelow isKindOfClass:[BZGInfoCell class]]) { return (BZGInfoCell *)cellBelow; } - + return nil; } @@ -110,14 +110,14 @@ - (void)showInfoCellBelowFormCell:(BZGTextFieldCell *)cell { NSUInteger cellIndex = [self.formCells indexOfObject:cell]; if (cellIndex == NSNotFound) return; - + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:cellIndex+1 inSection:self.formSection]; - + // if an info cell is already showing, do nothing BZGInfoCell *infoCell = [self infoCellBelowFormCell:cell]; if (infoCell) return; - + // otherwise, add the cell's info cell to the table view [self.formCells insertObject:cell.infoCell atIndex:cellIndex+1]; [self.tableView insertRowsAtIndexPaths:@[indexPath] @@ -128,11 +128,11 @@ - (void)removeInfoCellBelowFormCell:(BZGTextFieldCell *)cell { NSUInteger cellIndex = [self.formCells indexOfObject:cell]; if (cellIndex == NSNotFound) return; - + // if no info cell is showing, do nothing BZGInfoCell *infoCell = [self infoCellBelowFormCell:cell]; if (!infoCell) return; - + // otherwise, remove it [self.formCells removeObjectAtIndex:cellIndex+1]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:cellIndex+1 @@ -169,7 +169,7 @@ - (BZGTextFieldCell *)nextFormCell:(BZGTextFieldCell *)cell { NSUInteger cellIndex = [self.formCells indexOfObject:cell]; if (cellIndex == NSNotFound) return nil; - + for (NSUInteger i = cellIndex + 1; i < self.formCells.count; ++i) { UITableViewCell *cell = self.formCells[i]; if ([cell isKindOfClass:[BZGTextFieldCell class]]) { @@ -183,7 +183,7 @@ - (BZGTextFieldCell *)previousFormCell:(BZGTextFieldCell *)cell { NSUInteger cellIndex = [self.formCells indexOfObject:cell]; if (cellIndex == NSNotFound || cellIndex == 0) return nil; - + for (NSInteger i = cellIndex - 1; i >= 0; --i) { UITableViewCell *cell = self.formCells[i]; if ([cell isKindOfClass:[BZGTextFieldCell class]]) { @@ -231,7 +231,7 @@ - (void)textFieldDidBeginEditing:(UITextField *)textField if (cell.didBeginEditingBlock) { cell.didBeginEditingBlock(cell, textField.text); } - + NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; if (self.showsKeyboardControl) { @@ -246,12 +246,12 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang if (!cell) { return YES; } - + NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string]; if (cell.shouldChangeTextBlock) { shouldChange = cell.shouldChangeTextBlock(cell, newText); } - + [self updateInfoCellBelowFormCell:cell]; return shouldChange; } @@ -265,7 +265,7 @@ - (void)textFieldDidEndEditing:(UITextField *)textField if (cell.didEndEditingBlock) { cell.didEndEditingBlock(cell, textField.text); } - + [self updateInfoCellBelowFormCell:cell]; } @@ -276,11 +276,11 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField if (!cell) { return YES; } - + if (cell.shouldReturnBlock) { shouldReturn = cell.shouldReturnBlock(cell, textField.text); } - + BZGTextFieldCell *nextCell = [self nextFormCell:cell]; if (!nextCell) { [cell.textField resignFirstResponder]; @@ -288,7 +288,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField else { [nextCell.textField becomeFirstResponder]; } - + [self updateInfoCellBelowFormCell:cell]; return shouldReturn; } @@ -312,14 +312,14 @@ - (void)formCell:(BZGFormCell *)formCell didChangeValidationState:(BZGValidation - (void)keyboardWillShow:(NSNotification *)notification { CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; - + UIEdgeInsets contentInsets; if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) { contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height), 0.0); } else { contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.width), 0.0); } - + self.tableView.contentInset = contentInsets; self.tableView.scrollIndicatorInsets = contentInsets; }