SearchBar.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // SearchBar.swift
  3. // LoopFollow
  4. //
  5. // Created by Jonas Björkert on 2025-01-13.
  6. // Copyright © 2025 Jon Fawcett. All rights reserved.
  7. //
  8. import SwiftUI
  9. import UIKit
  10. struct SearchBar: UIViewRepresentable {
  11. @Binding var text: String
  12. class Coordinator: NSObject, UISearchBarDelegate {
  13. @Binding var text: String
  14. init(text: Binding<String>) {
  15. _text = text
  16. }
  17. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  18. text = searchText
  19. }
  20. func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  21. searchBar.resignFirstResponder()
  22. }
  23. }
  24. func makeCoordinator() -> Coordinator {
  25. return Coordinator(text: $text)
  26. }
  27. func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar {
  28. let searchBar = UISearchBar(frame: .zero)
  29. searchBar.placeholder = "Search Log"
  30. searchBar.delegate = context.coordinator
  31. searchBar.autocapitalizationType = .none
  32. searchBar.searchBarStyle = .minimal
  33. return searchBar
  34. }
  35. func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>) {
  36. uiView.text = text
  37. }
  38. }