Без опису

CustomNavigationView.swift 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // CustomNavigationView.swift
  3. // Comedores Sociales
  4. //
  5. // Created by Hector Carrion on 11/30/20.
  6. //
  7. import SwiftUI
  8. struct CustomNavigationView: UIViewControllerRepresentable {
  9. func makeCoordinator() -> Coordinator {
  10. return CustomNavigationView.Coordinator(parent: self)
  11. }
  12. // Just Change Your View That Requires Search Bar...
  13. var view: AnyView
  14. // Ease Of Use.....
  15. var largeTitle: Bool
  16. var title: String
  17. var placeHolder: String
  18. // onSearch And OnCancel Closures....
  19. var onSearch: (String)->()
  20. var onCancel: ()->()
  21. // requre closure on Call...
  22. init(view: AnyView,placeHolder: String? = "Search",largeTitle: Bool? = true,title: String,onSearch: @escaping (String)->(),onCancel: @escaping ()->()) {
  23. self.title = title
  24. self.largeTitle = largeTitle!
  25. self.placeHolder = placeHolder!
  26. self.view = view
  27. self.onSearch = onSearch
  28. self.onCancel = onCancel
  29. }
  30. // Integrating UIKit Navigation Controller With SwiftUI View...
  31. func makeUIViewController(context: Context) -> UINavigationController {
  32. // requires SwiftUI View...
  33. let childView = UIHostingController(rootView: view)
  34. let controller = UINavigationController(rootViewController: childView)
  35. // Nav Bar Data...
  36. controller.navigationBar.topItem?.title = title
  37. controller.navigationBar.prefersLargeTitles = largeTitle
  38. // search Bar....
  39. let searchController = UISearchController()
  40. searchController.searchBar.placeholder = placeHolder
  41. // setting delegate...
  42. searchController.searchBar.delegate = context.coordinator
  43. // setting Search Bar In NavBar...
  44. // disabling hide on scroll...
  45. // disabling dim bg..
  46. searchController.obscuresBackgroundDuringPresentation = false
  47. controller.navigationBar.topItem?.hidesSearchBarWhenScrolling = false
  48. controller.navigationBar.topItem?.searchController = searchController
  49. return controller
  50. }
  51. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
  52. // Updating Real Time...
  53. uiViewController.navigationBar.topItem?.title = title
  54. uiViewController.navigationBar.topItem?.searchController?.searchBar.placeholder = placeHolder
  55. uiViewController.navigationBar.prefersLargeTitles = largeTitle
  56. }
  57. // search Bar Delegate...
  58. class Coordinator: NSObject,UISearchBarDelegate{
  59. var parent: CustomNavigationView
  60. init(parent: CustomNavigationView) {
  61. self.parent = parent
  62. }
  63. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  64. // when text changes....
  65. self.parent.onSearch(searchText)
  66. }
  67. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  68. // when cancel button is clicked...
  69. self.parent.onCancel()
  70. }
  71. }
  72. }