Aucune description

CustomNavigationView.swift 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. var view: AnyView
  13. // Ease Of Use.....
  14. var largeTitle: Bool
  15. var title: String
  16. var placeHolder: String
  17. // onSearch And OnCancel Closures....
  18. var onSearch: (String)->()
  19. var onCancel: ()->()
  20. var onClick: ()->()
  21. // requre closure on Call...
  22. init(view: AnyView, placeHolder: String? = NSLocalizedString("search", comment:""), largeTitle: Bool? = true, title: String, onSearch: @escaping (String)->(), onCancel: @escaping ()->(), onClick: @escaping ()->()) {
  23. self.title = title // Inventory
  24. self.largeTitle = largeTitle!
  25. self.placeHolder = placeHolder!
  26. self.view = view
  27. self.onSearch = onSearch
  28. self.onCancel = onCancel
  29. self.onClick = onClick
  30. }
  31. // Integrating UIKit Navigation Controller With SwiftUI View...
  32. func makeUIViewController(context: Context) -> UINavigationController {
  33. // requires SwiftUI View...
  34. let childView = UIHostingController(rootView: view)
  35. let controller = UINavigationController(rootViewController: childView)
  36. // Nav Bar Data...
  37. controller.navigationBar.topItem?.title = title
  38. controller.navigationBar.prefersLargeTitles = largeTitle
  39. // search Bar....
  40. let searchController = UISearchController()
  41. searchController.searchBar.placeholder = placeHolder
  42. // setting delegate...
  43. searchController.searchBar.delegate = context.coordinator
  44. // setting Search Bar In NavBar...
  45. // disabling hide on scroll...
  46. // disabling dim bg..
  47. searchController.obscuresBackgroundDuringPresentation = false
  48. controller.navigationBar.topItem?.hidesSearchBarWhenScrolling = false
  49. controller.navigationBar.topItem?.searchController = searchController
  50. return controller
  51. }
  52. func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
  53. // Updating Real Time...
  54. uiViewController.navigationBar.topItem?.title = title
  55. uiViewController.navigationBar.topItem?.searchController?.searchBar.placeholder = placeHolder
  56. uiViewController.navigationBar.prefersLargeTitles = largeTitle
  57. }
  58. // search Bar Delegate...
  59. class Coordinator: NSObject,UISearchBarDelegate{
  60. var parent: CustomNavigationView
  61. init(parent: CustomNavigationView) {
  62. self.parent = parent
  63. }
  64. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  65. self.parent.onClick()
  66. }
  67. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  68. // when text changes....
  69. self.parent.onSearch(searchText)
  70. }
  71. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  72. // when cancel button is clicked...
  73. self.parent.onCancel()
  74. }
  75. }
  76. }