This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo ScreenOrientation
用于管理设备屏幕方向的通用库。
屏幕方向定义为图形在设备上绘制时所采用的方向。例如,下图中的设备分别处于垂直和水平的物理方向,但屏幕方向为纵向。有关物理设备方向,请参阅 Device Motion 的方向部分。
在 Android 和 iOS 平台上,屏幕方向的更改将覆盖任何系统设置或用户偏好。在 Android 上,可以在考虑用户首选方向的同时更改屏幕方向。在 iOS 上,应用无法访问用户和系统设置,且对屏幕方向的任何更改都会覆盖现有设置。
Web 支持有限。
安装
- npx expo install expo-screen-orientationIf you are installing this in an existing React Native app, make sure to install expo in your project.
警告
Apple 在 iOS 9 中为 iPad 添加了对 分屏视图 模式的支持。这改变了系统处理屏幕方向的方式。简而言之,在 iOS 中,除非你将两个应用并排打开,否则你的 iPad 始终处于横屏模式。要能够使用此模块锁定屏幕方向,你需要禁用对该功能的支持。有关 分屏视图 模式的更多信息,请查看Apple 官方文档。
app 配置中的配置
如果你在项目中使用配置插件(Continuous Native Generation (CNG)),你可以使用 expo-screen-orientation 内置的 config plugin 来进行配置。该插件允许你配置一些无法在运行时设置的属性,这些属性需要构建新的应用二进制文件后才会生效。如果你的应用不使用 CNG,那么你需要手动配置该库。
Example app.json with config plugin
{ "expo": { "ios": { "requireFullScreen": true }, "plugins": [ [ "expo-screen-orientation", { "initialOrientation": "DEFAULT" } ] ] } }
Configurable properties
| Name | Default | Description |
|---|---|---|
initialOrientation | undefined | Only for: iOS 设置 iOS 的初始屏幕方向。可选值: |
Are you using this library in an existing React Native app?
- 使用
xed ios在 Xcode 中打开 ios 目录。如果你没有该目录,运行npx expo prebuild -p ios来生成一个。 - 勾选 Xcode 中的
Requires Full Screen复选框。它位于 Project Target > General > Deployment Info 下。
使用 Expo Router 按屏幕设置屏幕方向
如果你使用 Expo Router,可以通过在 Stack.Screen 上使用 orientation 选项,为每个屏幕单独设置方向。这由 react-native-screens 提供支持,是在堆栈导航器中按屏幕设置方向的推荐方式。
import { Stack } from 'expo-router'; export default function Layout() { return ( <Stack> <Stack.Screen name="index" options={{ orientation: 'portrait' }} /> <Stack.Screen name="landscape" options={{ orientation: 'landscape' }} /> </Stack> ); }
API
import * as ScreenOrientation from 'expo-screen-orientation';
Methods
Gets the current screen orientation.
Promise<Orientation>Returns a promise that fulfils with an Orientation
value that reflects the current screen orientation.
Gets the current screen orientation lock type.
Promise<OrientationLock>Returns a promise which fulfils with an OrientationLock
value.
Gets the platform specific screen orientation lock type.
Promise<PlatformOrientationInfo>Returns a promise which fulfils with a PlatformOrientationInfo
value.
| Parameter | Type | Description |
|---|---|---|
| orientationLock | OrientationLock | The orientation lock to apply. See the |
Lock the screen orientation to a particular OrientationLock.
Promise<void>Returns a promise with void value, which fulfils when the orientation is set.
Example
async function changeScreenOrientation() { await ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.LANDSCAPE_LEFT); }
| Parameter | Type | Description |
|---|---|---|
| options | PlatformOrientationInfo | The platform specific lock to apply. See the |
Promise<void>Returns a promise with void value, resolving when the orientation is set and rejecting
if an invalid option or value is passed.
| Parameter | Type |
|---|---|
| orientationLock | OrientationLock |
Returns whether the OrientationLock policy is supported on
the device.
Promise<boolean>Returns a promise that resolves to a boolean value that reflects whether or not the
orientationLock is supported.
Sets the screen orientation back to the OrientationLock.DEFAULT policy.
Promise<void>Returns a promise with void value, which fulfils when the orientation is set.
Event subscriptions
| Parameter | Type | Description |
|---|---|---|
| listener | OrientationChangeListener | Each orientation update will pass an object with the new |
Invokes the listener function when the screen orientation changes from portrait to landscape
or from landscape to portrait. For example, it won't be invoked when screen orientation
change from portrait up to portrait down, but it will be called when there was a change from
portrait up to landscape left.
EventSubscriptionDeprecated: this function will be removed in a future version. Use
subscription.remove()instead.
| Parameter | Type | Description |
|---|---|---|
| subscription | EventSubscription | A subscription object that manages the updates passed to a listener function on an orientation change. |
Unsubscribes the listener associated with the Subscription object from all orientation change
updates.
voidDeprecated: this function will be removed in future versions. Keep track of your own subscriptions.
Removes all listeners subscribed to orientation change updates.
voidInterfaces
A subscription object that allows to conveniently remove an event listener from the emitter.
Types
| Property | Type | Description |
|---|---|---|
| orientationInfo | ScreenOrientationInfo | The current |
| orientationLock | OrientationLock | The current |
| Property | Type | Description |
|---|---|---|
| screenOrientationArrayIOS(optional) | Orientation[] | Only for: iOS An array of orientations to allow on the iOS platform. |
| screenOrientationConstantAndroid(optional) | number | Only for: Android A constant to set using the Android native API.
For example, in order to set the lock policy to unspecified,
|
| screenOrientationLockWeb(optional) | WebOrientationLock | Only for: Web A web orientation lock to apply in the browser. |
| Property | Type | Description |
|---|---|---|
| horizontalSizeClass(optional) | SizeClassIOS | Only for: iOS The horizontal size class of the device. |
| orientation | Orientation | The current orientation of the device. |
| verticalSizeClass(optional) | SizeClassIOS | Only for: iOS The vertical size class of the device. |
Enums
Orientation.UNKNOWN = 0An unknown screen orientation. For example, the device is flat, perhaps on a table.
An enum whose values can be passed to the lockAsync
method.
Note:
OrientationLock.ALLandOrientationLock.PORTRAITare invalid on devices which don't supportOrientationLock.PORTRAIT_DOWN.
OrientationLock.DEFAULT = 0The default orientation. On iOS, this will allow all orientations except Orientation.PORTRAIT_DOWN.
On Android, this lets the system decide the best orientation.
OrientationLock.OTHER = 8A platform specific orientation. This is not a valid policy that can be applied in lockAsync.
OrientationLock.UNKNOWN = 9An unknown screen orientation lock. This is not a valid policy that can be applied in lockAsync.
Each iOS device has a default set of size classes that you can use as a guide when designing your interface.
An enum representing the lock policies that can be applied on the web platform, modelled after
the W3C specification.
These values can be applied through the lockPlatformAsync
method.