美文网首页
2021-07-31

2021-07-31

作者: gengzx | 来源:发表于2021-07-31 18:26 被阅读0次

一、首先看一下iOS15之前的搜索框

搜索框需要自己写 功能等方面也需要自己实现

struct NewSearch: View {
    let names = ["Raju", "Ghanshyam", "Baburao Ganpatrao Apte", "Anuradha", "Kabira", "Chaman Jhinga", "Devi Prasad", "Khadak Singh"]
    
    @State private var searchTerm : String = ""
    
    var body: some View {

        NavigationView{
            List {
                SearchBar(text: $searchTerm)
                ForEach(self.names.filter{
                    self.searchTerm.isEmpty ? true : $0.localizedStandardContains(self.searchTerm)
                }, id: \.self) { name in
                    Text(name)
                }
            }
            .navigationBarTitle(Text("Search Bar"))
        }
    }
}

struct SearchBar : UIViewRepresentable {
    
    @Binding var text : String
    
    class Cordinator : NSObject, UISearchBarDelegate {
        
        @Binding var text : String
        
        init(text : Binding<String>) {
            _text = text
        }
        
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            text = searchText
        }
    }
    
    func makeCoordinator() -> SearchBar.Cordinator {
        return Cordinator(text: $text)
    }
    
    func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar {
        let searchBar = UISearchBar(frame: .zero)
        searchBar.delegate = context.coordinator
        return searchBar
    }
    
    func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>) {
        uiView.text = text
    }
}
image.pngimage.png

二、iOS15新增

struct ContentView: View {
    let names = ["Holly", "Josh", "Rhonda", "Ted"]
    @State private var searchText = ""

    var body: some View {
        
        NavigationView {
            List {
                ForEach(searchResults, id: \.self) { name in
                    NavigationLink(destination: Text(name)) {
                        Text(name)
                    }
                }
            }
            .searchable(text: $searchText) {
                ForEach(searchResults, id: \.self) { result in
                    Text("Are you looking for \(result)?").searchCompletion(result)
                }
            }
            .navigationTitle("Contacts")
        }
    }

    var searchResults: [String] {
        if searchText.isEmpty {
            return names
        } else {
            return names.filter { $0.contains(searchText) }
        }
    }
}

searchable 只能在导航控制器中使用,通过绑定的字符串可直接实现搜索

image.pngimage.png
在未实现搜索建议的时候,进行的是模糊搜索
            .searchable(text: $searchText, prompt: "搜索", suggestions: {
                ForEach(searchResults, id: \.self) { result in
                    Text("Are you looking for \(result)?").searchCompletion(result)
                }
            })

实现搜索建议 点击可以搜索某一项


image.pngimage.png

三、注意事项

iOS15新增的searchable 只能在 NavigationView 中使用

相关文章

  • 阅读打卡

    【日期】2021-07-31 【阅读书籍】山下英子《断舍离》 【阅读进度】10%—17% 【打卡天数】打卡17/2...

  • 《怒火·重案》观感

    作者:徐洁洁 观影时间:2021-07-31 01当感觉世界都背叛你了,你是否还能保持初心? 警匪片看过不少,其实...

  • 满目的花草

    幸福日志2021-07-31 周六 晴 李元胜有首诗,《我想和你虚度时光》,这是我常来读的一首,很爱。 记录这样一...

  • #Dairy269 重启

    2021-07-31 晴 周六 今天又赖床了,所以原本周六上午的采购硬生生因为我,下午才去。 外面风很大,甚至能被...

  • 【餐饮100问】61、有没有过放弃?

    D291 2021-07-31 七月底的最后一天,我们到了济南。月底的全盘,捎带着做了清洁,整理。 复盘一下七月,...

  • 当着孩子面撒谎,理由竟然是因为……

    2021-07-31 今天下午学校门口有个孩子抱着猫箱要往大门里走,门卫拦住她,除了查验证件以外,还提出:不许将动...

  • 2021-07-31

    2021.6.19 能有多痛呢,没有多痛的。 失去一个本就不值得的人,是一件很幸运的事,况且,因为这个,我可以重新...

  • 2021-07-31

    现在的人呀什么都不差就差钱! 现在不知是怎么了?每天来找我咨询的人多数都是为钱来的!之前为情所困的,为教育孩子的,...

  • 2021-07-31

    论生逢其时 葛小芬 青葱岁月寻金矿, 铁塔安装矗立艰。 好在英姿迎飒爽, 争当第一笑开颜。

  • 2021-07-31

    每天给自己一点时间沉淀吧! 以一颗淡然的心去看一切纷扰,过去的能忘则忘,眼前的能不计较就不计较,不乱于心。

网友评论

      本文标题:2021-07-31

      本文链接:https://www.haomeiwen.com/subject/twjkvltx.html