SwiftUI_visionOS_Note

Picker()

Jan 16, 2024
1 min read|
  • Picker() is an ideal component for any situation where a user needs to select options.
  • Different styles and formats can be applied, each better suited to either large or smaller data sets.

1. Write a default picker code

Picker(
	"Text",
	selection: .constant(1),
	content: {
		Text("1").tag(1)
	})
swift

2. Bind the string into selection

@State var selection: String = "1"
swift
  • change the tag number into string
Text("1").tag("1")...
swift

  • Create Text with Age and selected value in HStack
HStack{
	Text("Age:")
	Text(selection)
}
swift

3. Use ForEach instead of writing one by one.

ForEach(18..<100) { number in
	Text("\(number)").tag("\(number)")
}
swift
  • Now picker has a number from 18 to 99

4. Stylize the Picker

Subscribe to our newsletter

Get the latest news and updates from our team