swift macOS上可搜索修饰符的键命令

wydwbb8l  于 12个月前  发布在  Swift
关注(0)|答案(2)|浏览(130)

编辑

现在有一个solution用于iOS 17

原创帖子

iOS 15 SwiftUI 3.0中引入的新功能之一是.searchable视图修饰符。下面是它如何工作的一个简单示例。我想添加通过按下⌘F搜索的功能,因为大多数应用程序都有。我如何实现该功能?据我所知,.keyboardShortcut修改器只能用于Button视图,这是相当有限的。有没有办法或变通办法?谢谢!:)

struct ContentView: View {
    @State var searchText = ""
    private let fruits = ["apple", "banana", "plum", "grape"]
    
    var filteredFruits: [String] {
        if !searchText.isEmpty {
             return fruits.filter { $0.starts(with: searchText.lowercased()) }
        } else {
            return fruits
        }
    }

    var body: some View {
        NavigationView {
            List {
                ForEach(filteredFruits, id: \.self) { fruit in
                    Text(fruit.capitalized)
                }
            }
            .navigationBarTitle("Search")
            .searchable(text: $searchText) // Need to add key command to search (⌘F)
        }
    }
}

字符串

pengsaosao

pengsaosao1#

您可以使用SwiftUI-Introspect来获取底层的UISearchBar,然后创建一个由键盘快捷键触发的隐藏按钮来聚焦搜索栏。
下面的示例允许您执行BIFF以激活搜索栏。
范例:

struct ContentView: View {
    @State private var searchText = ""
    @State private var searchBar: UISearchBar?
    private let fruits = ["apple", "banana", "plum", "grape"]

    var filteredFruits: [String] {
        if !searchText.isEmpty {
             return fruits.filter { $0.starts(with: searchText.lowercased()) }
        } else {
            return fruits
        }
    }

    var body: some View {
        NavigationView {
            List {
                ForEach(filteredFruits, id: \.self) { fruit in
                    Text(fruit.capitalized)
                }
            }
            .navigationBarTitle("Search")
            .searchable(text: $searchText)
            .background(
                Button("Search fruits") {
                    focusSearchBar()
                }
                .keyboardShortcut("F", modifiers: .command)
                .hidden()
            )
        }
        .introspectNavigationController { nav in
            searchBar = nav.navigationBar.subviews.first { view in
                view is UISearchBar
            } as? UISearchBar
        }
    }

    private func focusSearchBar() {
        searchBar?.becomeFirstResponder()
    }
}

字符串
测试结果:


的数据

oipij1gg

oipij1gg2#

两年后,我在这里,我们得到了苹果的回应。
从iOS 17开始,您现在可以使用.searchable(text: Binding<String>, isPresented: Binding<Bool>)

struct ContentView: View {
    @State private var searchText = ""
    @State private var isSearching = false
    private let fruits = ["apple", "banana", "plum", "grape"]
    
    var filteredFruits: [String] {
        if !searchText.isEmpty {
             return fruits.filter { $0.starts(with: searchText.lowercased()) }
        } else {
            return fruits
        }
    }

    var body: some View {
        NavigationView {
            List {
                ForEach(filteredFruits, id: \.self) { fruit in
                    Text(fruit.capitalized)
                }
            }
            .navigationBarTitle("Search")
            .searchable(text: $searchText, isPresented: $isSearching)
            .background(Button("", action: { isSearching = true }).keyboardShortcut("f").hidden()) // some means to trigger the key command
        }
    }
}

字符串
希望对大家有帮助:)

相关问题