SwiftUI-使 View 可聚焦并在macOS上触发操作

标签 swift macos swiftui first-responder

如何使macOS上的View聚焦。我尝试了以下操作,但从未触发该操作。我知道对于NSView,您必须实现acceptsFirstResponder以返回true,但在SwiftUI中找不到类似的代码。

这仍然是与Beta相关的bug或macOS缺少的功能吗?

struct FocusableView: View {

    var body: some View {
        Group {
            Text("Hello World!")
                .padding(20)
                .background(Color.blue.cornerRadius(8))
            .focusable(true) { isFocused in
                print("Focused", isFocused)
            }
        }
    }
}

最佳答案

我能够使它起作用的唯一方法是选中“键盘系统偏好设置”中的“使用键盘导航在控件之间移动焦点”复选框

enter image description here

关于SwiftUI-使 View 可聚焦并在macOS上触发操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58049130/

相关文章:

swiftui - 如何防止 SwiftUI 中的工作表使其后面的 View 变暗或删除不必要的填充?

ios - Swift:如何使用链接库中的 Objective-C 函数?

ios - 如何快速避免 AFNetworking 导致的内存泄漏

swift - 如何在 Swift3 中从 DateFormatter() 获取 Int

macos - 我需要一种无需在 Mac 上安装即可运行 Haskell 代码的方法

ios - 从 SwiftUI 中的 `List` 删除顶部填充

ios - 解析 JSQMessagesViewController 缓存问题

macos - 错误 : The 'brew link' step did not complete successfully

macos - 如何通过音频组件服务发现 [非 Apple] 音频单元?

swift - 尝试在 SwiftUI 中使用时,实例方法 'appendInterpolation' 要求 'Decimal' 符合 '_FormatSpecifiable'