Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

BottomSheet

一个 SwiftUI BottomSheet 组件,可从屏幕底部呈现内容。

iOS
tvOS
Included in Expo Go
Recommended version:
~58.0.1

Expo UI BottomSheet 与官方 SwiftUI sheet API 相匹配,并从屏幕底部呈现内容

处于中等 detent 的 BottomSheet,显示一个“排序方式”列表,其中已选择“最新”处于中等 detent 的 BottomSheet,显示一个“排序方式”列表,其中已选择“最新”

安装

Terminal
- npx expo install @expo/ui
- yarn expo install @expo/ui
- pnpm expo install @expo/ui
- bun expo install @expo/ui

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

用法

基础底部工作表

将打开工作表的控件作为 anchor 属性传入。子元素就是工作表的内容

BasicBottomSheetExample.tsx
import { useState } from 'react'; import { Host, BottomSheet, Button, Text, VStack, } from '@expo/ui/swift-ui'; export default function BasicBottomSheetExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <VStack> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} anchor={ <Button label="Open sheet" onPress={() => setIsPresented(true)} /> }> <Text>Hello, world!</Text> </BottomSheet> </VStack> </Host> ); }

将 React Native 视图作为锚点

锚点可以是 React Native 视图。请将其包裹在 RNHostView 中

BottomSheetRNAnchorExample.tsx
import { useState } from 'react'; import { Pressable, Text as RNText } from 'react-native'; import { Host, BottomSheet, Button, RNHostView, Text, VStack, } from '@expo/ui/swift-ui'; export default function BottomSheetRNAnchorExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <VStack> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} fitToContents anchor={ <RNHostView matchContents> <Pressable onPress={() => setIsPresented(true)} style={{ backgroundColor: '#007AFF', padding: 12, borderRadius: 8, alignSelf: 'flex-start', }}> <RNText style={{ color: 'white', fontWeight: '600' }}> Open sheet </RNText> </Pressable> </RNHostView> }> <VStack> <Text>Opened from a React Native anchor.</Text> <Button label="Close" onPress={() => setIsPresented(false)} /> </VStack> </BottomSheet> </VStack> </Host> ); }

适应内容的底部工作表

使用 fitToContents 属性自动调整工作表大小,使其适应内容

BottomSheetFitsContentExample.tsx
import { useState } from 'react'; import { Host, BottomSheet, Button, Text, VStack, } from '@expo/ui/swift-ui'; export default function BottomSheetFitsContentExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <VStack> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} fitToContents anchor={ <Button label="Open sheet" onPress={() => setIsPresented(true)} /> }> <VStack> <Text> This sheet automatically sizes to fit its content. </Text> <Button label="Close" onPress={() => setIsPresented(false)} /> </VStack> </BottomSheet> </VStack> </Host> ); }

自定义背景

默认情况下,工作表使用系统的半透明材质背景(iOS 26 上为 Liquid Glass)。在 Group 上使用 presentationBackground 修饰符来自定义工作表背景,同时工作表也不会再使用该半透明材质。它接受任何 ShapeStyle:纯色、材质或渐变

BottomSheetBackgroundColorExample.tsx
import { useState } from 'react'; import { Host, BottomSheet, Button, Group, Text, VStack, } from '@expo/ui/swift-ui'; import { presentationBackground, presentationDetents, padding, foregroundStyle, } from '@expo/ui/swift-ui/modifiers'; export default function BottomSheetBackgroundColorExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <VStack> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} anchor={ <Button label="Open sheet" onPress={() => setIsPresented(true)} /> }> <Group modifiers={[ presentationDetents(['medium', 'large']), presentationBackground('#ffffff'), ]}> <VStack modifiers={[padding({ all: 20 })]}> <Text modifiers={[foregroundStyle('#000000')]}> Solid white sheet background. </Text> <Button label="Close" onPress={() => setIsPresented(false)} /> </VStack> </Group> </BottomSheet> </VStack> </Host> ); }

带有 presentation detent 的底部工作表

在 Group 上使用 presentationDetents 修饰符来控制可用高度。你可以使用:

  • 'medium':系统中等高度(约为屏幕的一半)
  • 'large':系统大高度(全屏)
  • { fraction: number }:屏幕高度的比例(0-1)
  • { height: number }:以点为单位的固定高度
BottomSheetWithDetentsExample.tsx
import { useState } from 'react'; import { Host, BottomSheet, Button, Group, Text, VStack, } from '@expo/ui/swift-ui'; import { presentationDetents } from '@expo/ui/swift-ui/modifiers'; export default function BottomSheetWithDetentsExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <VStack> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} anchor={ <Button label="Open sheet" onPress={() => setIsPresented(true)} /> }> <Group modifiers={[ presentationDetents([ 'medium', 'large', { fraction: 0.3 }, { height: 200 }, ]), ]}> <Text>This sheet can snap to multiple heights.</Text> </Group> </BottomSheet> </VStack> </Host> ); }

跟踪底部工作表的 detent 选择

将 selection 和 onSelectionChange 选项传递给 presentationDetents,以便以编程方式控制工作表吸附到哪个 detent

BottomSheetWithDetentSelectionExample.tsx
import { useState } from 'react'; import { Host, BottomSheet, Button, List, Section, Text, VStack, Group, } from '@expo/ui/swift-ui'; import { presentationDetents, presentationDragIndicator, foregroundStyle, } from '@expo/ui/swift-ui/modifiers'; import type { PresentationDetent } from '@expo/ui/swift-ui/modifiers'; export default function BottomSheetWithDetentSelectionExample() { const [isPresented, setIsPresented] = useState(false); const detents: PresentationDetent[] = [ { height: 300 }, { fraction: 0.3 }, 'medium', 'large', ]; const [selectedDetent, setSelectedDetent] = useState<PresentationDetent>('medium'); const formatDetent = (detent: PresentationDetent): string => { if (typeof detent === 'string') return detent; if ('fraction' in detent) return `Fraction ${detent.fraction}`; return `Height ${detent.height}`; }; return ( <Host style={{ flex: 1 }}> <VStack> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} anchor={ <Button label="Show sheet" onPress={() => setIsPresented(true)} /> }> <Group modifiers={[ presentationDetents(detents, { selection: selectedDetent, onSelectionChange: setSelectedDetent, }), presentationDragIndicator('visible'), ]}> <List> <Section title="Change detent"> <Button label="Height 300" onPress={() => setSelectedDetent({ height: 300 })} /> <Button label="Fraction 0.3" onPress={() => setSelectedDetent({ fraction: 0.3 }) } /> <Button label="Medium" onPress={() => setSelectedDetent('medium')} /> <Button label="Large" onPress={() => setSelectedDetent('large')} /> </Section> <Section title="Current"> <Text modifiers={[foregroundStyle('secondaryLabel')]}> {formatDetent(selectedDetent)} </Text> </Section> </List> </Group> </BottomSheet> </VStack> </Host> ); }

带有背景交互的底部工作表

使用 presentationBackgroundInteraction 修饰符,允许与工作表后面的内容进行交互

BottomSheetWithBackgroundInteractionExample.tsx
import { useState } from 'react'; import { Host, BottomSheet, Button, Group, Text, VStack, } from '@expo/ui/swift-ui'; import { presentationDetents, presentationBackgroundInteraction, } from '@expo/ui/swift-ui/modifiers'; export default function BottomSheetWithBackgroundInteractionExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <VStack> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} anchor={ <Button label="Open sheet" onPress={() => setIsPresented(true)} /> }> <Group modifiers={[ presentationDetents(['medium', 'large']), presentationBackgroundInteraction({ type: 'enabledUpThrough', detent: 'medium', }), ]}> <Text> Interact with content behind when at medium height. </Text> </Group> </BottomSheet> </VStack> </Host> ); }

不可关闭的底部工作表

使用 interactiveDismissDisabled 修饰符,防止用户通过滑动关闭工作表

NonDismissibleBottomSheetExample.tsx
import { useState } from 'react'; import { Host, BottomSheet, Button, Group, Text, VStack, } from '@expo/ui/swift-ui'; import { interactiveDismissDisabled } from '@expo/ui/swift-ui/modifiers'; export default function NonDismissibleBottomSheetExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <VStack> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} anchor={ <Button label="Open sheet" onPress={() => setIsPresented(true)} /> }> <Group modifiers={[interactiveDismissDisabled()]}> <VStack> <Text> This sheet cannot be dismissed by swiping. </Text> <Button label="Close" onPress={() => setIsPresented(false)} /> </VStack> </Group> </BottomSheet> </VStack> </Host> ); }

带有 React Native 内容的底部工作表

使用 RNHostView 将 React Native 组件嵌入底部工作表。设置 matchContents,使宿主视图自动调整大小以适应其内容

BottomSheetWithRNContentExample.tsx
import { useState } from 'react'; import { PlatformColor, Pressable, Text as RNText, View, } from 'react-native'; import { Host, BottomSheet, Button, Group, RNHostView, VStack, } from '@expo/ui/swift-ui'; import { presentationDragIndicator } from '@expo/ui/swift-ui/modifiers'; export default function BottomSheetWithRNContentExample() { const [isPresented, setIsPresented] = useState(false); const [counter, setCounter] = useState(0); return ( <Host style={{ flex: 1 }}> <VStack> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} fitToContents anchor={ <Button label="Open sheet" onPress={() => setIsPresented(true)} /> }> <Group modifiers={[presentationDragIndicator('visible')]}> <RNHostView matchContents> <View style={{ padding: 24 }}> <RNText style={{ fontSize: 18, fontWeight: 'bold', marginBottom: 8, color: PlatformColor('label'), }}> React Native Content </RNText> <RNText style={{ color: '#666', marginBottom: 16 }}> Counter: {counter} </RNText> <Pressable style={{ backgroundColor: '#007AFF', padding: 12, borderRadius: 8, alignItems: 'center', marginBottom: 12, }} onPress={() => setCounter(counter + 1)}> <RNText style={{ color: 'white', fontWeight: '600' }}> Increment </RNText> </Pressable> <Pressable style={{ backgroundColor: '#FF3B30', padding: 12, borderRadius: 8, alignItems: 'center', }} onPress={() => setIsPresented(false)}> <RNText style={{ color: 'white', fontWeight: '600' }}> Close </RNText> </Pressable> </View> </RNHostView> </Group> </BottomSheet> </VStack> </Host> ); }

带有可伸缩 React Native 内容的底部工作表

使用 flex: 1 的 React Native 内容时,请省略 RNHostView 上的 matchContents 属性,并使用 presentationDetents 控制工作表高度

BottomSheetWithFlexRNContentExample.tsx
import { useState } from 'react'; import { Text as RNText, View } from 'react-native'; import { Host, BottomSheet, Button, Group, RNHostView, VStack, } from '@expo/ui/swift-ui'; import { presentationDetents, presentationDragIndicator, } from '@expo/ui/swift-ui/modifiers'; export default function BottomSheetWithFlexRNContentExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <VStack> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} anchor={ <Button label="Open sheet" onPress={() => setIsPresented(true)} /> }> <Group modifiers={[ presentationDetents(['medium', 'large']), presentationDragIndicator('visible'), ]}> <RNHostView> <View style={{ flex: 1, backgroundColor: '#007AFF', padding: 24, }}> <RNText style={{ fontSize: 18, fontWeight: 'bold', color: 'white', }}> Flexible React Native Content </RNText> <RNText style={{ color: 'white', marginTop: 8 }}> This content fills the available space in the sheet. </RNText> </View> </RNHostView> </Group> </BottomSheet> </VStack> </Host> ); }

可滚动的 React Native 内容

使用 RNHostView 将可滚动的 React Native 列表(例如 FlatList、ScrollView,或 FlashList、Legend List 等高性能列表)嵌套在工作表中。使用 presentationDetents 设置工作表大小,列表会在该高度范围内滚动

BottomSheetWithScrollableContentExample.tsx
import { useState } from 'react'; import { FlatList, PlatformColor, Text as RNText, View, } from 'react-native'; import { Host, BottomSheet, Button, Group, RNHostView, VStack, } from '@expo/ui/swift-ui'; import { presentationDetents, presentationDragIndicator, } from '@expo/ui/swift-ui/modifiers'; const DATA = Array.from({ length: 50 }, (_, i) => `Item ${i + 1}`); export default function BottomSheetWithScrollableContentExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <VStack> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} anchor={ <Button label="打开工作表" onPress={() => setIsPresented(true)} /> }> <Group modifiers={[ presentationDetents(['medium', 'large']), presentationDragIndicator('visible'), ]}> <RNHostView> <View style={{ padding: 16 }}> <FlatList data={DATA} keyExtractor={item => item} renderItem={({ item }) => ( <RNText style={{ paddingVertical: 16, color: PlatformColor('label'), }}> {item} </RNText> )} /> </View> </RNHostView> </Group> </BottomSheet> </VStack> </Host> ); }

API

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

Component

BottomSheet

iOS
tvOS

Type: React.Element<BottomSheetProps>

BottomSheet presents content from the bottom of the screen.

BottomSheetProps

anchor

iOS
tvOS
Optional • Type: ReactNode

A view the sheet is anchored to, for example the Button that opens it. Rendered in place and kept mounted, so presenting the sheet doesn't shift surrounding layout. Optional.

children

iOS
tvOS
Type: ReactNode

The sheet's content, mounted while presented and unmounted after dismiss. Wrap it in Group to apply presentation modifiers.

fitToContents

iOS
tvOS
Optional • Type: boolean • Default: false

When true, the sheet will automatically size itself to fit its content. This sets the presentation detent to match the height of the children.

isPresented

iOS
tvOS
Type: boolean

Whether the BottomSheet is presented.

onDismiss

iOS
tvOS
Optional • Type: () => void

Callback function that is called after the BottomSheet has been fully dismissed.

onIsPresentedChange

iOS
tvOS
Type: (isPresented: boolean) => void

Callback function that is called when the BottomSheet presented state changes.