我正在努力为现有 macOS 代码库中的自定义 UI 元素添加可访问性。我们有一个自定义错误窗口,当错误发生时会出现该窗口,我正在尝试让 VoiceOver 在错误发生时宣布错误。
目前,我正在尝试使用 NSAccessibility.post(element: Any, notification: NSAccessibility.Notification, userInfo: [NSAccessibility.NotificationUserInfoKey : Any]?)
方法,但在运行代码时,不会发出通知。
这是我目前在 NSViewController 中得到的内容:
NSAccessibility.post(element: self, notification: .announcementRequested, userInfo: [
.announcement: NSLocalizedString("CANT_CONNECT_ERROR", comment: "Error string for connection failure"),
.priority: NSAccessibilityPriorityLevel.high
])
我的期望是,使用 .announcementRequested
选项,VoiceOver 应自动选择该选项并说出公告,但运气不佳。我尝试过使用不同的通知类型,尝试在 .announcement
参数中放入硬编码字符串值,并尝试使用 .rawValue
和 90
对于 .priority
参数。
最佳答案
经过多次尝试和错误(以及 danielpunkass 的有用评论),我终于弄清楚了如何让它发挥作用。 YMMV,但我是这样让它工作的:
element
参数必须设置为NSApp.mainWindow
(我还添加了as Any
来消除编译器警告)userInfo
字典中的优先级必须使用.rawValue
设置,而不是实际的枚举值。 Apple's documentation当然没有提到这一点。
这是我的工作代码:
NSAccessibility.post(
element: NSApp.mainWindow as Any,
notification: .announcementRequested,
userInfo: [
.announcement: "This is a custom accessibility notification",
.priority: NSAccessibilityPriorityLevel.high.rawValue
]
)
关于swift - VoiceOver 不会宣布 NSAccessibility 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57045146/