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

1

7

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

Vincentvincenta answered 18/2, 2020 at 5:5 Comment(1)
It seems SegmentedPickerStyle does not show the title. You need to create a custom title for it.Yulan
N
6

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())
}
Nemathelminth answered 18/2, 2020 at 6:1 Comment(3)
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.Vincentvincenta
@Ravindra_Bhati, that is a question to Apple )) Probably such their vision of segmented picker. You can always submit feedback to Apple.Nemathelminth
The label / String that you pass in the Picker is also used by screen readers to read it @VincentvincentaHypocrisy

© 2022 - 2024 — McMap. All rights reserved.