Advertisement
Milotronik

Nav + State

Oct 1st, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. public extension View {
  2. func navigationAppearance1(standardAppearance: NavigationBarAppearance,
  3. scrollAppearance: NavigationBarAppearance,
  4. currentAppearance: Binding<NavigationBarAppearance>) -> some View {
  5. modifier(NavigationAppearanceViewModifier1(standardAppearance: standardAppearance,
  6. scrollAppearance: scrollAppearance,
  7. currentAppearance: currentAppearance))
  8. }
  9. }
  10.  
  11. private struct NavigationAppearanceViewModifier1: ViewModifier {
  12. let standardAppearance: NavigationBarAppearance
  13. let scrollAppearance: NavigationBarAppearance
  14. @Binding var currentAppearance: NavigationBarAppearance
  15.  
  16. func body(content: Content) -> some View {
  17. content.background(NavigationAppearanceRepresentable1(standardAppearance: standardAppearance,
  18. scrollAppearance: scrollAppearance,
  19. currentAppearance: $currentAppearance))
  20. }
  21. }
  22.  
  23. private struct NavigationAppearanceRepresentable1: UIViewControllerRepresentable {
  24. let standardAppearance: NavigationBarAppearance
  25. let scrollAppearance: NavigationBarAppearance
  26. @Binding var currentAppearance: NavigationBarAppearance
  27.  
  28. func makeUIViewController(
  29. context: UIViewControllerRepresentableContext<NavigationAppearanceRepresentable1>
  30. ) -> NavigationAppearanceViewController1 {
  31. let viewController = NavigationAppearanceViewController1(standardAppearance: standardAppearance,
  32. scrollAppearance: scrollAppearance)
  33. viewController.onAppearanceChange = { appearance in
  34. currentAppearance = appearance
  35. }
  36. return viewController
  37. }
  38.  
  39. func updateUIViewController(
  40. _ uiViewController: NavigationAppearanceViewController1,
  41. context: UIViewControllerRepresentableContext<NavigationAppearanceRepresentable1>
  42. ) {
  43. uiViewController.update(standardAppearance: standardAppearance, scrollAppearance: scrollAppearance)
  44. }
  45. }
  46.  
  47. private final class NavigationAppearanceViewController1: UIViewController {
  48. let standardAppearance: NavigationBarAppearance
  49. let scrollAppearance: NavigationBarAppearance
  50. var onAppearanceChange: ((NavigationBarAppearance) -> Void)?
  51.  
  52. init(standardAppearance: NavigationBarAppearance,
  53. scrollAppearance: NavigationBarAppearance) {
  54. self.standardAppearance = standardAppearance
  55. self.scrollAppearance = scrollAppearance
  56. super.init(nibName: nil, bundle: nil)
  57. }
  58.  
  59. required init?(coder: NSCoder) {
  60. fatalError("init(coder:) has not been implemented")
  61. }
  62.  
  63. override func viewWillAppear(_ animated: Bool) {
  64. super.viewWillAppear(animated)
  65. if let navigationController = navigationController {
  66. navigationController.style(standardAppearance: standardAppearance, scrollAppearance: scrollAppearance)
  67. onAppearanceChange?(standardAppearance)
  68. }
  69. }
  70.  
  71. func update(standardAppearance: NavigationBarAppearance,
  72. scrollAppearance: NavigationBarAppearance) {
  73. if let navigationController = navigationController {
  74. navigationController.style(standardAppearance: standardAppearance, scrollAppearance: scrollAppearance)
  75. onAppearanceChange?(scrollAppearance)
  76. }
  77. }
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement