0

I have this simple code:

import SwiftUI

struct ExampleView: View {

    @State private var selectedTab: String = "tab1"

    var body: some View {
        VStack {
            Picker("Mode", selection: $selectedTab) {
                Text("tab1").tag("tab1")
                Text("tab2").tag("tab2")
                Text("tab3").tag("tab3")
            }
            .pickerStyle(.segmented)
            .padding([.horizontal, .top])
        
            Form {
                switch selectedTab {
                    case "tab1":
                        TabOneView()
                    case "tab2":
                        TabTwoView()
                    case "tab3":
                        TabThreeView()
                    default:
                        TabOneView()
                }
            }
        }
        .navigationTitle("Title")
        .navigationBarTitleDisplayMode(.inline)
    }
}

When I try this in the preview it works correctly, but when I use it in the app, where this View is called with a NavigationLink from another View the picker seems freeze: pressing another option does not change the value and so also the view does not change (also visually, the default option remains selected).

The parent View:

struct ParentView: View {

    var body: some View {
        Form {
            // some other code
        
            Section(header: Text("SUBVIEWS")) {
                NavigationLink {
                    ExampleView()
                } label: {
                    Text("Example")
                }
                NavigationLink {
                    Example2View()
                } label: {
                    Text("Example2")
                }
            }
        }
        .navigationTitle("ParentView")
        .navigationBarTitleDisplayMode(.inline)
    }
}

I also tried to print the values with in method .onChange on the variable selectedTab, but it does not print anything (synonym that the value is not changed).

This is how it looks like: enter image description here

2
  • 1
    Please show the real code then - the code "where this View is called with a NavigationLink from another View". Commented Dec 17, 2024 at 14:51
  • I still can't reproduce the issue after implementing the last edit. Commented Dec 17, 2024 at 16:10

1 Answer 1

0

Using .zIndex(1) on the Picker solves the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.