Skip to content

Commit 3949457

Browse files
committed
Tweaked "next input view" search
1 parent 6f43dee commit 3949457

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

TPKeyboardAvoiding/UIScrollView+TPKeyboardAvoidingAdditions.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,16 @@ - (void)TPKeyboardAvoiding_findNextInputViewAfterView:(UIView*)priorView beneath
199199
// Search recursively for input view below/to right of priorTextField
200200
CGRect priorFrame = [self convertRect:priorView.frame fromView:priorView.superview];
201201
CGRect candidateFrame = *bestCandidate ? [self convertRect:(*bestCandidate).frame fromView:(*bestCandidate).superview] : CGRectZero;
202-
CGFloat bestCandidateHeuristic = -sqrt(candidateFrame.origin.x*candidateFrame.origin.x + candidateFrame.origin.y*candidateFrame.origin.y)
203-
+ (fabs(CGRectGetMinY(candidateFrame) - CGRectGetMinY(priorFrame)) < FLT_EPSILON ? 1e6 : 0);
202+
CGFloat bestCandidateHeuristic = [self TPKeyboardAvoiding_nextInputViewHeuristicForViewFrame:candidateFrame];
204203

205204
for ( UIView *childView in view.subviews ) {
206205
if ( [self TPKeyboardAvoiding_viewIsValidKeyViewCandidate:childView] ) {
207206
CGRect frame = [self convertRect:childView.frame fromView:view];
208207

209-
// Use a heuristic to evaluate candidates: prefer elements closest to the top left, and on the same line
210-
CGFloat heuristic = -sqrt(frame.origin.x*frame.origin.x + frame.origin.y*frame.origin.y)
211-
+ (fabs(CGRectGetMinY(frame) - CGRectGetMinY(priorFrame)) < FLT_EPSILON ? 1e6 : 0);
208+
// Use a heuristic to evaluate candidates
209+
CGFloat heuristic = [self TPKeyboardAvoiding_nextInputViewHeuristicForViewFrame:frame];
210+
211+
NSLog(@"%@: (%lfx%lf, %lf)", ((UITextField*)childView).placeholder, frame.origin.x, frame.origin.y, heuristic);
212212

213213
// Find views beneath, or to the right. For those views that match, choose the view closest to the top left
214214
if ( childView != priorView
@@ -225,6 +225,11 @@ - (void)TPKeyboardAvoiding_findNextInputViewAfterView:(UIView*)priorView beneath
225225
}
226226
}
227227

228+
- (CGFloat)TPKeyboardAvoiding_nextInputViewHeuristicForViewFrame:(CGRect)frame {
229+
return (-frame.origin.y * 1000.0) // Prefer elements closest to top (most important)
230+
+ (-frame.origin.x); // Prefer elements closest to left
231+
}
232+
228233
- (BOOL)TPKeyboardAvoiding_viewIsValidKeyViewCandidate:(UIView *)view {
229234
if ( view.hidden || !view.userInteractionEnabled ) return NO;
230235

0 commit comments

Comments
 (0)