Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI - Segment Control with Lists

Im trying to make a Segment Control that handles Lists and/or Vstacks

I was able to make a Segment Control with text, but not Lists


import SwiftUI

struct MaterialSegmentControl : View {
    @State private var MaterialType = 0

    var body: some View {

        NavigationView {

            VStack {
                SegmentedControl(selection: $MaterialType) {
                    Text("Style").tag(0)
                    Text("Text").tag(1)
                    Text("Arrange").tag(2)

                }
                Text("Value: \(MaterialType)")


            }
        }
    }
}

How can I have a Segment Control that switched between Lists and/or Vstacks?

like image 434
Mane Manero Avatar asked Jun 20 '19 16:06

Mane Manero


1 Answers

Yes! It's pretty straightforward. Something like this:

struct MaterialSegmentControl : View {
    @State private var MaterialType = 0

    var body: some View {

        NavigationView {

            VStack {
                SegmentedControl(selection: $MaterialType) {
                    Text("Style").tag(0)
                    Text("Text").tag(1)
                    Text("Arrange").tag(2)
                }

                if MaterialType == 0 {
                    List {
                        Text("Hi")
                        Text("\(MaterialType)")
                    }
                } else if MaterialType == 1 {
                    List {
                        Text("Beep")
                        Text("\(MaterialType)")
                    }
                } else {
                    List {
                        Text("Boop")
                        Text("\(MaterialType)")
                    }
                }
            }
        }
    }
}
like image 200
piebie Avatar answered Oct 20 '22 22:10

piebie