0

I am fairly new to SwiftUI and trying to figure out how to achieve trailing alignment for the Picker view selection. Eg for UIKit, in a normal UITableView cell, you can have a Right Detail cell with a disclosure indicator.

I have tried a few other solutions here, here, here essentially HStack with Spacer, without hstack, without spacers, frame alignments, combination of fixedSize, etc., and haven't had any success. I am fairly certain that I am missing on something really stupid and simple.

Here's the code that I am using

class SwiftUITestData: ObservableObject {
    @Published var selection: String
    init(selection: String) {
        self.selection = selection
    }
}

struct SwiftUITestContainer: View {
    @ObservedObject var selectionData: SwiftUITestData
    var body: some View {
        SwiftUITest(selection: $selectionData.selection)
    }
}

struct SwiftUITest: View {
    @Binding var selection: String
    var dummyTextArray = ["Reference contains scanned text", "Blah blah blah blah blah", "blah2 blah2 blah2 blah2"]
    var body: some View {
        Form {
            Section() {
                HStack {
                    Text("Type").fixedSize()
                    Spacer()
                    Picker("", selection: $selection) {
                        ForEach(0..<dummyTextArray.count) { index in
                            Text(dummyTextArray[index])
                        }
                    }
                    .padding(.top, 5)
                    .padding(.bottom, 5)
                    .multilineTextAlignment(.trailing)
                }
                
            }
        }
    }
}

#Preview {
    SwiftUITestContainer(selectionData: SwiftUITestData(selection: "Reference contains scanned text"))
}

Following is what I am seeing with the above code.

Output enter image description here

Following is what I desire with the trailing alignment

Desired Result

Ignore the chevron, the above example is purely for alignment of the right detail. The drop-down selection is what I desire along with trailing alignment for selection.

3
  • I can't reproduce the issue. Using your code, the picker text is right-aligned for me. What device and iOS version are you using? Commented Jul 19, 2024 at 3:16
  • That said, this might help anyway. Commented Jul 19, 2024 at 3:17
  • @Sweeper iPhone 14 pro, iOS 17 is the device. The screenshots are from iPhone 15 pro preview. You need to have longer texts to witness the behavior. If you have one-word text or smaller text that can fit in the space, you get a trailing alignment. This is the reason why I tried multiline alignment as well. Commented Jul 19, 2024 at 15:35

1 Answer 1

0

Sounds like a problem picker style can solve for you. You can use .navigationLink if you want the chevron to the right and navigate the user to a new screen to make the selection. Here's an example on how you can do that:

 class SwiftUITestData: ObservableObject {
    @Published var selection: String
    init(selection: String) {
        self.selection = selection
    }
}

struct SwiftUITestContainer: View {
    @ObservedObject var selectionData: SwiftUITestData
    var body: some View {
        SwiftUITest(selection: $selectionData.selection)
    }
}

struct SwiftUITest: View {
    @Binding var selection: String
    var dummyTextArray = ["Reference contains scanned text", "Blah blah blah blah blah", "blah2 blah2 blah2 blah2"]
    var body: some View {
        NavigationStack {
            Form {
                Section {
                        Picker("Type", selection: $selection) {
                            ForEach(0..<dummyTextArray.count) { index in
                                Text(dummyTextArray[index])
                            }
                        }
                        .pickerStyle(.navigationLink)
                        .padding(.top, 5)
                        .padding(.bottom, 5)
                        .multilineTextAlignment(.trailing)
                    }
                }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Mozz, thank you for providing the answer. I should have been more clear wrt to chevron (edited the question) on the table view cell from UIKit. That was just an example from UIKit, I am more focused on alignment for the right label, which has trailing alignment. I still want the drop-down in screen selection. Navigation link requires me to push to a separate screen
In that case Menu is your friend. SwiftUI Picker is not very customizable so you are limited to the default styling options that come with it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.