辅助功能:ScrollView自动滚动到视图,在点击“TAB”时看不到

时间:2017-12-14 10:21:10

标签: cocoa accessibility nsscrollview

有人让我知道当仅限键盘的用户尝试使用“Tab”键在ScrollView中的不同UI元素之间导航时,如何自动滚动scrollView?当我点击“TAB”键时,焦点将转移到scrollView中存在的不同UI元素,但如果UI元素不在可见内容视图中,则它不会滚动。如何实现这一目标。帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:6)

解决方案A:创建NSWindow的子类并覆盖makeFirstResponder:。第一个响应者更改时会调用makeFirstResponder

- (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;
}

解决方案B:创建NSTextField和其他控件的子类并覆盖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;
}