Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picker with SegmentPickerStyle() doesn’t show picker title in SwiftUI

Tags:

I created a segment picker for select gender in that segment picker I have to option one for Male and another for Female. Picker is working fine But set title on with picker it’s not showing. I set title Select Gender which is not showing as a title on the Picker.

struct ManagerChildrenView: View {

        //MARK: Properties
        @State var selectedGender = 0
        let genders = ["Male", "Female"]

        var body: some View {

            Form{
                Section{
                    Picker("Select Gender", selection: $selectedGender) {

                        ForEach(0..<genders.count) { index in
                            Text(self.genders[index]).tag(index).font(.title)
                        }
                    }.pickerStyle(SegmentedPickerStyle())
                }
            }
        }
    }

Screenshot:

enter image description here

like image 877
Ravindra_Bhati Avatar asked Feb 18 '20 05:02

Ravindra_Bhati


1 Answers

Label is not used for SegmentedPickerStyle. Instead you can use the following approach

enter image description here

HStack {
    Text("Select Gender")
    Picker("", selection: $selectedGender) {

        ForEach(0..<genders.count) { index in
            Text(self.genders[index]).tag(index).font(.title)
        }
    }.pickerStyle(SegmentedPickerStyle())
}
like image 86
Asperi Avatar answered Sep 30 '22 15:09

Asperi