cocoa - 辅助功能:ScrollView 自动滚动到点击 "TAB"时不可见的 View

标签 cocoa accessibility nsscrollview

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

最佳答案

解决方案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;
}

关于cocoa - 辅助功能:ScrollView 自动滚动到点击 "TAB"时不可见的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47810930/

相关文章:

objective-c - Cocoa - 读取文件的内容?

css - 为什么文本缩进辅助功能不会受到搜索引擎的惩罚?

objective-c - OS X 上的 CATiledLayers

xcode - Mountain Lion 和 AuthorizationExecuteWithPrivileges 警告

macos - 绑定(bind) nsrect 和 nstextfield

objective-c - 如何在 Mac 应用程序中显示字体预览?

html - html5中的<figure>标签可以用于背景图片吗?

ios - 测试 VoiceOver : how can I verify that my UIAccessibilityLayoutChangedNotification notifications are working?

objective-c - NSScrollView 内的 NSImageView 平铺在缩小时绘制不需要的边框

objective-c - NSMutableArray 只初始化一次