Search code examples
iosswiftswiftuiswiftui-list

How to automatically detect NavigationBarTitle displayMode become .inline or .large in SwiftUI?


I have a List inside a navigationView and I want to change the List Section text whenever the list is scrolled and the navigationBarTitle become .inline or .large

This is my code:

import SwiftUI

struct ContentView: View {

@State private var scrolledUp = false

var body: some View {

    NavigationView {

        if scrolledUp {

            List {
                Section(header: Text("Moved UP"))
                {
                    Text("Line1").bold()
                    Text("Line2").bold()
                    Text("Line2").bold()

                }

                .navigationBarTitle("Setting")
            }

        } else {

            List {
                Section(header: Text("Not Moved"))
                {
                    Text("Line1").bold()
                    Text("Line2").bold()
                    Text("Line2").bold()

                }
            }
            .navigationBarTitle("Setting")
        }  
     }
   }
}

how can I find out the list is scrolled and the navigationBar is changed to .title?


Solution

  • For now I used geometryReader to detect the y position of the List section header. During the scroll. If the position is less than 80, the navigationBarTitle is changed to .title.

    It works perfectly. However, I still looking for a better solution.