Reference version

Expo 启动屏幕 iconExpo 启动屏幕

一个提供访问并控制原生启动屏幕可见性行为的库。

Android
iOS
tvOS
Bundled version:
~31.0.13

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

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

SDK 52 开始,由于支持最新 Android 启动屏 API 的变化,Expo Go 和开发构建无法完整复现你的 独立应用 中用户会看到的启动屏体验。Expo Go 会显示你的应用图标而不是启动屏,开发构建中的启动屏也不会反映配置插件中设置的所有属性。强烈建议你在发布构建上测试启动屏,以确保其外观符合预期。

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

安装

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 />; }
App.tsx
import { Text, View } from 'react-native'; import Entypo from '@expo/vector-icons/Entypo'; import * as SplashScreen from 'expo-splash-screen'; // 设置动画选项。这是可选的。 SplashScreen.setOptions({ duration: 1000, fade: true, }); export default function App() { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text>启动屏演示!👋</Text> <Entypo name="rocket" size={30} /> </View> ); }

延迟隐藏启动屏

在某些情况下,可能需要延迟隐藏启动屏,直到某些资源加载完成。例如,如果你需要在显示应用内容之前加载 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 />; }
App.tsx
import { useCallback, useEffect, useState } from 'react'; import { Text, View } from 'react-native'; import Entypo from '@expo/vector-icons/Entypo'; import * as SplashScreen from 'expo-splash-screen'; // 在我们获取资源时保持启动屏可见 SplashScreen.preventAutoHideAsync(); export default function App() { const [isReady, setIsReady] = useState(false); useEffect(() => { async function doAsyncStuff() { try { // 在这里执行一些异步操作 } catch (e) { console.warn(e); } finally { setIsReady(true); } } doAsyncStuff(); }, []); useEffect(() => { if (isReady) { SplashScreen.hideAsync(); } }, [isReady]); if (!isReady) { return null; } return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text>启动屏演示!👋</Text> <Entypo name="rocket" size={30} /> </View> ); }

配置

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

如下所示,使用配置插件是配置启动屏的推荐方式。 其他方法现在都被视为旧方法,并将在未来被移除。

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

你也可以使用以下 应用配置 属性来配置 expo-splash-screen,但应优先使用配置插件。

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';

Methods

SplashScreen.hide()

Android
iOS
tvOS

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()

Android
iOS
tvOS

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()

Android
iOS
tvOS

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

Important note: It is recommended to call this in global scope without awaiting, rather than inside React components or hooks, because otherwise this might be called too late, when the splash screen is already hidden.

Returns:
Promise<boolean>

Example

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

SplashScreen.setOptions(options)

Android
iOS
tvOS
ParameterType
optionsSplashScreenOptions

Configures the splashscreens default animation behavior.

Returns:
void

Types

SplashScreenOptions

Android
iOS
tvOS
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