6

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Ravindra_Bhati
  • 1,071
  • 13
  • 28
  • It seems `SegmentedPickerStyle` does not show the title. You need to create a custom title for it. – FRIDDAY Feb 18 '20 at 05:54

1 Answers1

5

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())
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • 1
    thanks, but what's the mean of title in the picker with SegmentedPickerStyle() . Why i add new View in the form of text on Screen. – Ravindra_Bhati Feb 18 '20 at 06:26
  • 2
    @Ravindra_Bhati, that is a question to Apple )) Probably such their vision of segmented picker. You can always submit feedback to Apple. – Asperi Feb 18 '20 at 06:30
  • 1
    The label / String that you pass in the `Picker` is also used by screen readers to read it @Ravindra_Bhati – Viraj Doshi Jan 25 '21 at 04:55