-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSearchBar.swift
45 lines (36 loc) · 1.26 KB
/
SearchBar.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// SearchBar.swift
//
// Created by Speedyfriend67 on 27.06.24
//
import SwiftUI
struct SearchBar: UIViewRepresentable {
@Binding var text: String
var onSearchButtonClicked: (() -> Void)?
class Coordinator: NSObject, UISearchBarDelegate {
@Binding var text: String
var onSearchButtonClicked: (() -> Void)?
init(text: Binding<String>, onSearchButtonClicked: (() -> Void)?) {
_text = text
self.onSearchButtonClicked = onSearchButtonClicked
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
text = searchText
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
onSearchButtonClicked?()
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(text: $text, onSearchButtonClicked: onSearchButtonClicked)
}
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
}
}