Reference version

选择器

一个用于从列表中选择选项的 SwiftUI Picker 组件。

iOS
tvOS
Included in Expo Go
Bundled version:
~56.0.6

For the complete documentation index, see llms.txt. Use this file to discover all available pages.

信息 有关跨平台用法,请参阅通用的 Picker — 它会为每个平台渲染 შესაბამის的原生组件。

Expo UI Picker 与官方 SwiftUI Picker API 保持一致,并通过 pickerStyle 修饰符支持所有选择器样式。

菜单样式的 Picker,显示水果选项,并勾选当前选择项

安装

Terminal
npx expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

分段式选择器

SegmentedPickerExample.tsx
import { useState } from 'react'; import { Host, Picker, Text } from '@expo/ui/swift-ui'; import { pickerStyle, tag } from '@expo/ui/swift-ui/modifiers'; const options = ['Apple', 'Banana', 'Orange']; export default function SegmentedPickerExample() { const [selectedTag, setSelectedTag] = useState(options[0]); return ( <Host matchContents> <Picker modifiers={[pickerStyle('segmented')]} label="选择一种水果" selection={selectedTag} onSelectionChange={selection => { setSelectedTag(selection); }}> {options.map(option => ( <Text key={option} modifiers={[tag(option)]}> {option} </Text> ))} </Picker> </Host> ); }

菜单选择器

MenuPickerExample.tsx
import { useState } from 'react'; import { Host, Picker, Text } from '@expo/ui/swift-ui'; import { pickerStyle, tag } from '@expo/ui/swift-ui/modifiers'; const options = ['Apple', 'Banana', 'Orange']; export default function MenuPickerExample() { const [selectedTag, setSelectedTag] = useState(options[0]); return ( <Host matchContents> <Picker modifiers={[pickerStyle('menu')]} label="选择一种水果" selection={selectedTag} onSelectionChange={selection => { setSelectedTag(selection); }}> {options.map(option => ( <Text key={option} modifiers={[tag(option)]}> {option} </Text> ))} </Picker> </Host> ); }

滚轮选择器

警告 滚轮变体在 Apple TV 上不可用。

WheelPickerExample.tsx
import { useState } from 'react'; import { Host, Picker, Text } from '@expo/ui/swift-ui'; import { pickerStyle, tag } from '@expo/ui/swift-ui/modifiers'; const options = ['Apple', 'Banana', 'Orange']; export default function WheelPickerExample() { const [selectedTag, setSelectedTag] = useState(options[0]); return ( <Host matchContents> <Picker modifiers={[pickerStyle('wheel')]} label="选择一种水果" selection={selectedTag} onSelectionChange={selection => { setSelectedTag(selection); }}> {options.map(option => ( <Text key={option} modifiers={[tag(option)]}> {option} </Text> ))} </Picker> </Host> ); }

API

import { Picker } from '@expo/ui/swift-ui';

Component

Picker

iOS
tvOS

Type: React.Element<PickerProps<T>>

Displays a native picker component

Example

<Picker modifiers={[pickerStyle('segmented')]}> <Text modifiers={[tag('option1')]}>Option 1</Text> <Text modifiers={[tag(0)]}>Option 3</Text> </Picker>

PickerProps

children

iOS
tvOS
Optional • Type: React.ReactNode

The content of the picker. You can use Text components with tag modifiers to display the options.

label

iOS
tvOS
Optional • Literal type: union

A label displayed on the picker.

Acceptable values are: string | React.ReactNode

onSelectionChange

iOS
tvOS
Optional • Type: (selection: T) => void

Callback function that is called when an option is selected. Gets called with the selected tag value.

selection

iOS
tvOS
Optional • Type: T

The selected option's tag modifier value.

systemImage

iOS
tvOS
Optional • Type: SFSymbol

The name of the system image (SF Symbol). For example: 'photo', 'heart.fill', 'star.circle'