Accessibility: ScrollView to automatically scroll to a view that does not appear when you press "TAB",

Can someone tell me how I can automatically scroll scrollView when a user using only the keyboard tries to navigate between the various user interface elements in ScrollView using the Tab key? When I press the "TAB" key, the focus shifts to another user interface element present in scrollView, but does not scroll if the user interface element is not in the visible content view. How can this be achieved. Help would be appreciated. Thanks.

+6
source share
1 answer

Solution A: Create a subclass NSWindowand override makeFirstResponder:. makeFirstRespondercalled when the first responder changes.

- (BOOL)makeFirstResponder:(NSResponder *)responder {
    BOOL madeFirstResponder = [super makeFirstResponder:responder];
    if (madeFirstResponder) {
        id view = [self firstResponder];
        // check if the new first responder is a field editor
        if (view && [view isKindOfClass:[NSTextView class]] && [view isFieldEditor])
            view = [view delegate]; // the control, usually a NSTextField
        if (view && [view isKindOfClass:[NSControl class]] && [view enclosingScrollView]) {
            NSRect rect = [view bounds];
            rect = NSInsetRect(rect, -10.0, -10.0); // add a margin
            [view scrollRectToVisible:rect];
        }
    }
    return madeFirstResponder;
}

Solution B: Subclass NSTextFieldand other controls and override becomeFirstResponder.

- (BOOL)becomeFirstResponder {
    BOOL becameFirstResponder = [super becomeFirstResponder];
    if (becameFirstResponder) {
        if ([self enclosingScrollView]) {
            NSRect rect = [self bounds];
            rect = NSInsetRect(rect, -10.0, -10.0); // add a margin
            [self scrollRectToVisible:rect];
        }
    }
    return becameFirstResponder;
}
+3
source

All Articles