SwiftUI macOS Scroll a List With Arrow Keys While a TextField is Active

依然范特西╮ 提交于 2020-06-12 15:23:00

问题


I'd like to use a SwiftUI TextField and a SwiftUI List to render a "search box" above a list of items. Something roughly like the search box available in Safari's Help menu item...which provides a search box where you can always enter text while simultaneously browsing through the list of results using the up and down arrow keys.

I've played with onMoveCommand, focusable, and adjustments to the "parent" NSWindow, but haven't found a clear and obvious way for the TextField to constantly accept input while still being able to navigate the underlying List using the up and down arrow keys. The following code allows for either text to be entered in the TextField, or list entries to be navigated through, but not both at the same time...

struct ContentView: View {

  @State var text: String = ""
  @State var selection: Int? = 1

  var body: some View {
    VStack {
      TextField("Enter text", text: $text)
      List(selection: $selection) {
        ForEach((1...100), id: \.self) {
          Text("\($0)")
        }
      }
    }
  }
}

来源:https://stackoverflow.com/questions/59764338/swiftui-macos-scroll-a-list-with-arrow-keys-while-a-textfield-is-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!