2

So I've got a NavigationView embedded inside of a TabView that is in the Page View Style. Upon the first load, the NavigationView will start with its title lower than where it should be. Once I reload the view, by going to a different tab, it resets its self correctly. Is there a way that I can fix this issue so that it starts in the correct position? I've made a GIF to better illustrate the problem:

My GIF

And here is my code:

struct ContentView: View {
    
    var body: some View {
        TabView {
            SettingsView()
            BlankView(color: .green)
            BlankView(color: .blue)
            BlankView(color: .red)
        }
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
        
    }
}

struct SettingsView: View {
    
    var body: some View {
        NavigationView {
            List {
                Section(header: Text("General")) {
                    NavigationLink(destination: SettingsItem(title: "1")) {
                        Text("1")
                    }
                    
                    NavigationLink(destination: SettingsItem(title: "2")) {
                        Text("2")
                    }
                    
                    NavigationLink(destination: SettingsItem(title: "3")) {
                        Text("3")
                    }
                    
                    NavigationLink(destination: SettingsItem(title: "4")) {
                        Text("4")
                    }
                    
                }
            }
            .listStyle(InsetGroupedListStyle())
            .navigationTitle("Settings")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct SettingsItem: View {
    
    @State var title = "Title"
    
    var body: some View {
        NavigationView {
            List {
                
            }
        }
        .navigationTitle(title)
    }
}

struct BlankView: View {
    let color: Color
    
    var body: some View {
        ZStack{
            color
            Text("Blank View")
                .padding()
        }
        .border(Color.black, width: 8)
        
    }
}
Mr Duck
  • 115
  • 1
  • 9
  • What happens if you add `.navigationViewStyle(StackNavigationViewStyle())` under `List` instead of `NavigationView`? – thecoolwinter Aug 23 '21 at 02:11
  • the problem seems to be `.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))` if you comment it or use `.tabViewStyle(.automatic)` it works without the issue, well at least for me. – workingdog support Ukraine Aug 23 '21 at 02:26
  • It looks like you have the same problem with Apple's Mail. – El Tomato Aug 23 '21 at 02:27
  • "... same problem with Apple's Mail" ??? P.S. you probably don't need another `NavigationView` in `SettingsItem`. – workingdog support Ukraine Aug 23 '21 at 02:36
  • So, @thecoolwinter this "works" but creates the same problem as seen: https://stackoverflow.com/questions/68844307/swiftui-navigationview-starting-inside-itself. – Mr Duck Aug 23 '21 at 02:42
  • @workingdog, this does not work as I get the error: Cannot infer contextual base in reference to member 'automatic.' And thank you, you are correct I do not need another NavigationView in SettingsItem. – Mr Duck Aug 23 '21 at 02:45
  • so if you remove `.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))` it does not work for you? `.automatic` is for newer ios. What system are you on? – workingdog support Ukraine Aug 23 '21 at 02:54
  • If I remove it and replace it with the .automatic the error that I mentioned above comes up. And I'm still on iOS 14. :/ I'd also like my app to be compatible with previous iOS versions too. – Mr Duck Aug 23 '21 at 03:04
  • my question was if you remove `.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))` does it work for you? – workingdog support Ukraine Aug 23 '21 at 03:10
  • 1
    just let us know if it works or not, then I'm done here. I don't do chat, sorry. – workingdog support Ukraine Aug 23 '21 at 03:15
  • Sorry. I had no idea what I was getting into, StackOverflow just prompted it at me. If I remove it, it solves the problem. BUT it also eliminates the style that the modifier provided, making the solution not ideal. – Mr Duck Aug 23 '21 at 03:18
  • 1
    @MrDuck Looks like a SwiftUI bug; `PageTabViewStyle(indexDisplayMode:)` messing with tabs having a `NavigationView`. – staticVoidMan Aug 23 '21 at 07:21
  • Is there a way that I can report a SwiftUI bug? – Mr Duck Aug 25 '21 at 22:00

0 Answers0