SwiftUI 替换UISegmentController和SegmentController的Picker分段控制器

效果图:


截屏2020-09-03 10.08.13.png
import SwiftUI

struct ContentView: View {
    let personArr: Array = ["小学", "初中", "高中", "大学", "社会人"]
    @State var selectedIndex = 0
    var body: some View {
        VStack{
            Text(personArr[selectedIndex])
            Picker(selection: $selectedIndex, label: Text("")) {
                ForEach(0 ..< personArr.count) { index in
                    Text(self.personArr[index])
                }
            }
            .pickerStyle(SegmentedPickerStyle())
            .padding()
            .onReceive([self.selectedIndex].publisher.first()) { (value) in
                //获取到当前点击的下标 do something
                print(value)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

你可能感兴趣的:(SwiftUI 替换UISegmentController和SegmentController的Picker分段控制器)