Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI picker with no title

I'm trying to add a simple picker functionality by SwiftUI. Essentially the outcome I'm looking for is something like this: enter image description here

When clicking on the arrow, it should take user to a new view where they select the required unit.

I tried something like this:

var units = ["ltr", "usg", "impg"]

@State private var selectedUnit = 0

var body: some View {
    Form {
        Section {
            VStack {
                Picker(selection: $selectedUnit, label: Text("")) {
                    ForEach(0 ..< units.count) {
                        Text(self.units[$0])
                        
                    }
                }.frame(width: 42)
            }
            
        }
    }.navigationBarTitle("Select unit")
}

But the as soon as this is wrapped in a form, it requires the title etc. and takes up too much space. I simply want to have the units as my base view. Is there any way to accomplish this?

like image 657
DevB1 Avatar asked Oct 16 '25 13:10

DevB1


2 Answers

Picker("", selection: $data) {
    ForEach(data, id:\self) { child in
        Text(child)
    }
}
.pickerStyle(InlinePickerStyle())
.labelsHidden()

Try .labelsHidden()

like image 113
gklka Avatar answered Oct 19 '25 07:10

gklka


Pass an EmptyView instance as the label of Picker to hide the title at top:

Picker(selection: $selectedUnit, label: EmptyView()) {
    ForEach(0 ..< units.count) {
        Text(self.units[$0])
    }
}
like image 42
Dorukhan Arslan Avatar answered Oct 19 '25 07:10

Dorukhan Arslan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!