0

I have a Picker within a Form. I don't want to show any label on this Picker, so I set the label to EmptyView(). Though, the UI still presents an empty space where the label would be. I'm looking for a way to remove this space.

enter image description here

var body: some View {
        NavigationView{
            Form {
...                
                Section(header: Text("Emails")){
                    HStack{
                        Picker(selection: $phoneType, label: EmptyView()){
                            ForEach(phoneTypes, id: \.self){
                                Text($0)
                            }
                        }
                        .pickerStyle(.navigationLink)
                        Spacer()
                        Link("[email protected]", destination: URL(string: "mailto:[email protected]")!)
                    }
                    Button("Add Email") {}
                }
...   
            }
        }
        .navigationTitle("John Doe")
    }
3
  • Which version of iOS are you using? Commented Jan 3, 2023 at 12:21
  • @darshilsakhiya 16.2 Commented Jan 3, 2023 at 12:21
  • 1
    @LudovicC You need to add the navigationTitle view modifier to your Form, not on NavigationView. Commented Jan 3, 2023 at 13:26

3 Answers 3

1

The correct approach is to use labelsHidden() to hide the label of Picker.

Picker(selection: $phoneType, label: EmptyView()){
    ForEach(phoneTypes, id: \.self){
        Text($0)
    }
}
.labelsHidden()
.pickerStyle(.navigationLink)
Sign up to request clarification or add additional context in comments.

Comments

0

A possible workaround is using Picker inside Menu:

                    HStack{
                        Menu {
                            Picker("", selection: $phoneType){
                                ForEach(["Work","Home","Cell"], id: \.self){ type in
                                    Button(type) { phoneType = type }
                                }
                            }
                        } label: {
                            Text(phoneType).foregroundColor(.secondary)
                        }
                        
                        Spacer()
                        Link("[email protected]", destination: URL(string: "mailto:[email protected]")!)
                    }

Comments

0

Put the Text in the loop inside an HStack and add a trailing Spacer

var body: some View {
    NavigationView{
        Form {
...                
            Section(header: Text("Emails")){
                HStack{
                    Picker(selection: $phoneType, label: EmptyView()){
                        ForEach(phoneTypes, id: \.self){ t in
                            HStack {
                                Text(t)
                                Spacer()
                            }
                        }
                    }
                    .pickerStyle(.navigationLink)
                    Spacer()
                    Link("[email protected]", destination: URL(string: "mailto:[email protected]")!)
                }
                Button("Add Email") {}
            }
...   
        }
    }
    .navigationTitle("John Doe")
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.