1

I have a Text and Picker in HStack separated by Spacer. What I want is that the Spacer should should occupy maximum space between picker and text so that the Text and Picker are not the extreme ends of the HStack but, the picker expands to occupy maximum space. Any clue as to how can this can achieved and that the size of Picker is equivalent to its content?

HStack() {
                Text(template.name)
                    .multilineTextAlignment(.leading)
                Spacer()
                if template.oneItemOnly == "1" {
                    if let items = template.items {
                        Picker(selection: $selectedSegment, label:Text("")){
                            ForEach( 0..<items.count) { index in
                                Text(items[index].name)
                                    .tag(index)
                            }
                        }
                        .pickerStyle(SegmentedPickerStyle())
                    }
                }
            }

This is how it looks

1 Answer 1

3

You can set max width of Spacer by adding a .frame modifier:

Spacer().frame(maxWidth: .infinity)

Alternatively you can try using GeometryReader to set the width of the Picker relative to the screen width:

GeometryReader { proxy in
    ...
    Picker(selection: $selectedSegment, label: EmptyView()) {
        ...
    }
    .frame(width: proxy.size.width / 3)
}

You may also try to implement your own Picker like presented here: Custom Segmented Picker

Sign up to request clarification or add additional context in comments.

2 Comments

This causes the Spacer and Picker to occupy equal width.
@Rakesh.P You may try using GeometryReader in this case.

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.