我想使用 SwiftUITextField https://developer.apple.com/documentation/swiftui/textfield和一个 SwiftUIList https://developer.apple.com/documentation/swiftui/list在项目列表上方呈现“搜索框”。大致类似于 Safari 的“帮助”菜单项中提供的搜索框...它提供了一个搜索框,您可以随时在其中输入文本,同时使用向上和向下箭头键浏览结果列表。
我玩过onMoveCommand https://developer.apple.com/documentation/swiftui/view/3367875-onmovecommand, focusable https://developer.apple.com/documentation/swiftui/textfield/3277076-focusable,以及对“父”的调整NSWindow
,但还没有找到一个清晰可见的方法TextField
不断接受输入,同时仍然能够导航底层List
使用向上和向下箭头键。以下代码允许在TextField
,或列出要导航的条目,但不能同时...
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)")
}
}
}
}
}
您可以将 NSTextField 包装在 NSViewRepresentable 中,并使用 NSTextFieldDelegate 来拦截向上和向下移动键。你可以看看我的建议演示。
带有建议的文本字段的来源 https://github.com/smic/SuggestionsDemo/blob/main/SuggestionsDemo/SuggestionTextField.swift
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)