Advertisement
Milotronik

Navigation Bar title

Oct 1st, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.04 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct ContentView: View {
  4.     @State private var titleColor: UIColor = .black
  5.  
  6.     var body: some View {
  7.         NavigationView {
  8.             ScrollView {
  9.                 ScrollViewReader { proxy in
  10.                     VStack {
  11.                         ForEach(0..<100) { index in
  12.                             Text("Item \(index)")
  13.                                 .padding()
  14.                         }
  15.                     }
  16.                     .background(GeometryReader { geo in
  17.                         Color.clear
  18.                             .preference(key: ScrollOffsetPreferenceKey.self, value: geo.frame(in: .global).minY)
  19.                     })
  20.                 }
  21.             }
  22.             .onPreferenceChange(ScrollOffsetPreferenceKey.self) { value in
  23.                 if value < 0 {
  24.                     titleColor = .red // Change to your desired color when scrolling
  25.                 } else {
  26.                     titleColor = .black // Change to your desired color when not scrolling
  27.                 }
  28.             }
  29.             .navigationBarTitle("Title", displayMode: .inline)
  30.             .navigationBarColor(titleColor)
  31.         }
  32.     }
  33. }
  34.  
  35. struct ScrollOffsetPreferenceKey: PreferenceKey {
  36.     typealias Value = CGFloat
  37.     static var defaultValue: CGFloat = 0
  38.     static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
  39.         value = nextValue()
  40.     }
  41. }
  42.  
  43. extension View {
  44.     func navigationBarColor(_ color: UIColor) -> some View {
  45.         self.modifier(NavigationBarModifier(backgroundColor: .clear, titleColor: color))
  46.     }
  47. }
  48.  
  49. struct NavigationBarModifier: ViewModifier {
  50.     var backgroundColor: UIColor?
  51.     var titleColor: UIColor?
  52.  
  53.     init(backgroundColor: UIColor?, titleColor: UIColor?) {
  54.         self.backgroundColor = backgroundColor
  55.         self.titleColor = titleColor
  56.         let coloredAppearance = UINavigationBarAppearance()
  57.         coloredAppearance.configureWithTransparentBackground()
  58.         coloredAppearance.backgroundColor = backgroundColor
  59.         coloredAppearance.titleTextAttributes = [.foregroundColor: titleColor ?? .black]
  60.         coloredAppearance.largeTitleTextAttributes = [.foregroundColor: titleColor ?? .black]
  61.  
  62.         UINavigationBar.appearance().standardAppearance = coloredAppearance
  63.         UINavigationBar.appearance().compactAppearance = coloredAppearance
  64.         UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
  65.     }
  66.  
  67.     func body(content: Content) -> some View {
  68.         ZStack {
  69.             content
  70.             VStack {
  71.                 GeometryReader { geometry in
  72.                     Color.clear
  73.                         .preference(key: NavigationBarHeightPreferenceKey.self, value: geometry.size.height)
  74.                 }
  75.                 Spacer()
  76.             }
  77.         }
  78.     }
  79. }
  80.  
  81. struct NavigationBarHeightPreferenceKey: PreferenceKey {
  82.     typealias Value = CGFloat
  83.     static var defaultValue: CGFloat = 0
  84.     static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
  85.         value = nextValue()
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement