Search code examples
swiftxcodeswiftuiswiftui-navigationview

Does anyone know what this stack is called SwfitUI?


I'll keep it quick: Does anyone know if SwiftUI have a built in method that renders something like this image: enter image description here

where the view just changes based on what label you tap? I wonder if it's possible to achieve this using some sort of navigation view or stack? I'd appreciate any input! Thanks.

EDIT:

            HStack {
                
                Picker(selection: $selected, label: Text("Mode"), content:{
                    Text("Projects").tag(1)
                    Text("Notes").tag(2)
                    Text("Docus").tag(3)
                }).pickerStyle(SegmentedPickerStyle())
                    .frame(width: 200, height: 10)
                    .padding(EdgeInsets(top: 0, leading: 0, bottom: 10, trailing: -0))
                
                if selected == 1 {
           // how would i show another view if the user selects option 1?
                }
                
                
            }

Solution

  • This is a picker. to be precise, this is a segmented picker.

    You can create it like so:

    struct ContentView: View {
        @State private var favoriteColor = 0
    
        var body: some View {
                Picker("What is your favorite color?", selection: $favoriteColor) {
                    Text("Red").tag(0)
                    Text("Green").tag(1)
                    Text("Blue").tag(2)
                }
                .pickerStyle(.segmented)
        }
    }
    

    When we create the picker we pass in a binding (when we change the picker's value it will know to switch to it) this is the thing with the dollar sign ($)

    The next thing is to add the segments. So we add text views with a tag attached to each one.

    Lastly we need to set the picker style (in this case the segmented)

    .pickerStyle(.segmented)
    

    I suggest you to look here: Create a segmented control and read values from it

    Hope this helps!