2

I have an input form with multiple TextFields to which I want add a Picker to select an enum value describing the type of organisation. The nearest I can get to what I want is the default style which produces the result shown here:

enter image description here

The UI code for the picker is as follows:

        LabeledContent("Organisation type") {
            Picker("", selection: $tradingEntity.tradingEntityType) {
                ForEach(OrganisationType.allCases, id: \.self) { value in
                    Text(value.friendlyString)
                        .tag(value)
                }
            }
            .frame(maxWidth: .infinity)
            .background()
        }

I cannot find any way to style the selected value, I really want to match the style of the TextFields by left justifying the selected value text with foreground colour of .primary. The list value styling is OK but I would prefer the list frame to be left justified.

The PickerStyle protocol has the following implementation methods but there is no supporting documentation help on how to implement them.

static func _makeView<SelectionValue>(value: _GraphValue<_PickerValue<ModifiedPickerStyle, SelectionValue>>, inputs: _ViewInputs) -> _ViewOutputs where SelectionValue : Hashable {
    // code
}

static func _makeViewList<SelectionValue>(value: _GraphValue<_PickerValue<ModifiedPickerStyle, SelectionValue>>, inputs: _ViewListInputs) -> _ViewListOutputs where SelectionValue : Hashable {
    // code
}

How can I implement theses methods to create the desired style or is there a simpler way to achieve the result I'm after?

3
  • 1
    Pickers are not very customizable. If you tried the predefined options, and none of them works for you, you can always create your custom overlay with VStack or List and style it any way you want. Commented Aug 10, 2023 at 14:20
  • Those methods are considered private you should use them there is no provided way to modify the built in picker. Commented Aug 10, 2023 at 16:43
  • Bummer! Thaks for the info. @timbre timbre - thanks for the custom overlay idea, I hadn't considered that. Commented Aug 10, 2023 at 17:14

2 Answers 2

1

From the comments to my original question it is clear that developers are only supposed to use the styles provided by Apple. However, I have found a way to get close to what I was looking for. All I did was wrap the original VStack in a Form view builder which then presented my UI as follows:

enter image description here

Personally, I prefer all the changeable values to have the sane style (.primary in this case) but, once again, there seems to be no way to atyle the active part of the control. Unless anyone can suggest any way to get closer to my requirement, i guess I'll have to make do with this.

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

Comments

0

Try using .tint(.primary) to change the Color to Primary

Preview

Picker("Hello", selection: $string) {
                        Text("Hi")
                            .tag("Hello")
                    }
                    .pickerStyle(.menu)
                    .tint(.primary)

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.