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.

Expo 启动画面 iconExpo 启动画面

一个提供控制原生启动画面可见性行为访问能力的库。

Android
iOS
tvOS
Recommended version:
~57.0.0

expo-splash-screen 库中的 SplashScreen 模块提供了对原生启动屏幕行为的控制。默认情况下,当你的应用准备就绪时,启动屏幕会自动隐藏,但你也可以为更高级的使用场景手动控制其可见性。

另请参阅 创建启动屏幕图片 的指南,或 使用浏览器快速生成图标和启动屏幕

安装

Terminal
npx expo install expo-splash-screen

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

用法

对于大多数应用,你无需对启动画面执行任何特殊操作。应用准备就绪后,启动画面会自动隐藏。你也可以选择配置动画选项:

app/_layout.tsx
import { Stack } from 'expo-router'; import * as SplashScreen from 'expo-splash-screen'; // 设置动画选项。此项为可选。 SplashScreen.setOptions({ duration: 1000, fade: true, }); export default function RootLayout() { return <Stack />; }

延迟隐藏启动画面

在某些情况下,你可能希望延迟隐藏启动画面,直到特定资源加载完成。例如,如果你需要在显示应用内容之前加载 API 数据,可以使用 preventAutoHideAsync() 手动控制启动画面的隐藏时机。目标应该是尽早隐藏启动画面。

app/_layout.tsx
import { Stack } from 'expo-router'; import * as SplashScreen from 'expo-splash-screen'; import { useEffect, useState } from 'react'; // 获取资源时保持启动画面可见 SplashScreen.preventAutoHideAsync(); export default function RootLayout() { const [isReady, setIsReady] = useState(false); useEffect(() => { async function doAsyncStuff() { try { // 在此处执行一些异步操作 } catch (e) { console.warn(e); } finally { setIsReady(true); } } doAsyncStuff(); }, []); useEffect(() => { if (isReady) { SplashScreen.hide(); } }, [isReady]); if (!isReady) { return null; } return <Stack />; }

配置

如果你在项目中使用配置插件(持续原生生成(CNG)),你可以使用 expo-splash-screen 内置的 config plugin 进行配置。该插件允许你配置一些无法在运行时设置、且需要构建新的应用二进制文件后才会生效的属性。如果你的应用使用 CNG,那么你需要手动配置该库。

建议使用如下所示的 config plugin 来配置启动屏幕。 其他方法现在都被视为旧版方案,并将在未来移除。

Example app.json with config plugin

app.json
{ "expo": { "plugins": [ [ "expo-splash-screen", { "backgroundColor": "#232323", "image": "./assets/splash-icon.png", "dark": { "image": "./assets/splash-icon-dark.png", "backgroundColor": "#000000" }, "imageWidth": 200 } ] ], } }

Configurable properties

NameDefaultDescription
backgroundColor#ffffff

表示启动屏幕背景颜色的十六进制颜色字符串。

imageundefined

将在启动屏幕上显示的图像文件路径。这应该是你的应用图标或徽标。

enableFullScreenImage_legacyfalse
Only for:
iOS

启用此属性后,可以将全屏图像用作启动屏幕。这有助于从旧版启动屏幕配置过渡,但未来将被移除。

darkundefined

包含用于在设备处于深色模式时配置启动屏幕属性的对象。

imageWidth100

设置图像的宽度。

androidundefined

包含用于配置 Android 上启动屏幕属性的对象。

iosundefined

包含用于配置 iOS 上启动屏幕属性的对象。

resizeModeundefined

决定图像如何缩放以适应由 imageWidth 定义的容器。可选值:containcovernative

Are you using this library in an existing React Native app?

请参阅 expo-splash-screen 仓库中的安装说明,了解如何配置原生项目。

为启动屏幕添加动画

SplashScreen 提供了开箱即用的淡入淡出动画。可以使用 setOptions 方法进行配置。

SplashScreen.setOptions({ duration: 1000, fade: true, });

如果你更喜欢使用自定义动画,请查看 with-splash-screen 示例,了解如何为启动屏幕应用任意动画。你可以通过运行 npx create-expo-app --example with-splash-screen,基于此示例初始化一个新项目。

API

import * as SplashScreen from 'expo-splash-screen';

Props

android

Only for:
Android

Optional • Type: Partial<AndroidSplashConfig>

Properties for configuring the splash screen on Android.

backgroundColor

Optional • Type: string • Default: "#ffffff"

Hex color for the splash screen background.

dark

Optional • Type: { backgroundColor: string, image: string }

Properties for configuring the splash screen in dark mode.

enableFullScreenImage_legacy

Optional • Type: boolean • Default: false

Whether to use a full screen image as the splash screen. Legacy transition helper, will be removed.

image

Optional • Type: string

Path to the image displayed on the splash screen.

imageWidth

Optional • Type: number • Default: 100

The width to make the image.

ios

Only for:
iOS

Optional • Type: Partial<IOSSplashConfig>

Properties for configuring the splash screen on iOS.

resizeMode

Optional • Literal type: string • Default: "contain"

How the image is scaled. Accepts contain, cover, native.

Acceptable values are: 'contain' | 'cover' | 'native'

Methods

SplashScreen.hide()

Hides the native splash screen immediately. Be careful to ensure that your app has content ready to display when you hide the splash screen, or you may see a blank screen briefly. See the "Usage" section for an example.

Returns:
void

SplashScreen.hideAsync()

Hides the native splash screen immediately. This method is provided for backwards compatability. See the "Usage" section for an example.

Returns:
Promise<void>

SplashScreen.preventAutoHideAsync()

Makes the native splash screen (configured in app.json) remain visible until hideAsync is called.

Returns:
Promise<boolean>

Example

import * as SplashScreen from 'expo-splash-screen'; SplashScreen.preventAutoHideAsync(); export default function App() { // ... }

SplashScreen.setOptions(options)

ParameterType
optionsSplashScreenOptions

Configures the splashscreens default animation behavior.

Returns:
void

Types

SplashScreenOptions

PropertyTypeDescription
duration(optional)number

The duration of the fade out animation in milliseconds.

Default:400
fade(optional)boolean
Only for:
iOS

Whether to hide the splash screen with a fade out animation.

Default:false