UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SwiftUI (Xcode 12.2) Picker no longer shows label.

Forums > SwiftUI

No matter how I format the Picker's label it won't show.

For example...

import SwiftUI

struct ContentView: View { @State private var selected = 0 var fruits = ["Apple", "Orange", "Mango"]

var body: some View {
    VStack {
        Picker(selection: $selected, label: Text("Select Any One Fruit")) {
            ForEach(0 ..< fruits.count) { index in
                Text(self.fruits[index]).tag(index)
            }
        }.pickerStyle(WheelPickerStyle())
        .padding()
        Text("Selected Value: \(fruits[selected])")
    }
}

}

It works EXCEPT there is no label "Select Any One Fruit" on the left side.

Did Apple can Picker labels? If I remove the label I get an error that a missing argument for parameter #1 in call.

Thanks!

3      

The label is required for a couple of reason

  1. it need for accessibilty (voice over)
  2. when useing Form without WheelPickerStyle() it place the label in

The label would not format properly before so I always used .labelHidden() anyway

3      

So the Label not showing is purposeful?

I was about to open a topic asking, because theres no way i can make it appear in the Lesson from Day 16 - SwiftUI.

Whats the alternative if I wanna a label with my picker?

3      

I would use a VStack with Text

struct ContentView: View {
    @State private var selectedFruit = "Apple"
    var fruits = ["Apple", "Orange", "Mango"]

    var body: some View {
        VStack {
            VStack(alignment: .leading) {
                Text("Select Any One Fruit")

                Picker(selection: $selectedFruit, label: Text("Select Any One Fruit")) {
                    ForEach(fruits, id: \.self) {
                        Text($0)
                    }
                }
                .pickerStyle(WheelPickerStyle())
            }
            .padding()

            Text("Selected Value: \(selectedFruit)")
        }
    }
}

Or use the Form (with WheelPickerStyle())

struct ContentView: View {
    @State private var selectedFruit = "Apple"
    var fruits = ["Apple", "Orange", "Mango"]

    var body: some View {
        Form {
            Text("Select Any One Fruit")

            Picker(selection: $selectedFruit, label: Text("Select Any One Fruit")) {
                ForEach(fruits, id: \.self) {
                    Text($0)
                }
            }
            .pickerStyle(WheelPickerStyle())

            Text("Selected Value: \(selectedFruit)")
        }
    }
}

However if using Form without WheelPickerStyle() then the label: will be used there. See which one you like better, however with only three choice I might go for SegmentedPickerStyle()

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.