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 ScreenOrientation iconExpo ScreenOrientation

用于管理设备屏幕方向的通用库。

Android
iOS
Web
Included in Expo Go
Recommended version:
~58.0.0

屏幕方向是指图形在设备上绘制的方向。例如,下图中的设备处于纵向和横向两种物理方向,但屏幕方向为纵向。有关设备的物理方向,请参阅 Device Motion 的方向部分。

在 Android 和 iOS 平台上,屏幕方向的更改会覆盖任何系统设置或用户偏好。在 Android 上,可以在考虑用户偏好的方向的同时更改屏幕方向。在 iOS 上,应用无法访问用户和系统设置,因此任何屏幕方向更改都会覆盖现有设置。

iOS 27 及更高版本中的方向锁定 
iOS

从 iOS 27 开始,iPhone 应用可以调整大小,例如在 macOS 上使用 iPhone Mirroring 时,或在 iPad 上运行时。当应用可调整大小时,系统会将受支持的方向视为偏好而非要求。这会带来两个后果:

  • lockAsync 可能不起作用。系统可以拒绝该请求,而应用仍可调整大小。
  • getOrientationAsync 可能无法反映应用所绘制窗口的形状。无论窗口的宽高比如何,iPhone Mirroring 始终报告纵向方向。

请设计能够适应可用空间的屏幕,而不要依赖固定方向。在调试版本中,如果系统拒绝方向锁定,此库会记录警告。利用这些警告找出仍依赖平台不再保证的方向锁定的屏幕。

安装

Terminal
- npx expo install expo-screen-orientation
- yarn expo install expo-screen-orientation
- pnpm expo install expo-screen-orientation
- bun expo install expo-screen-orientation

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

警告

Apple 在 iOS 9 中为 iPad 添加了对 分屏视图 模式的支持。这改变了系统处理屏幕方向的方式。简而言之,对于 iOS,除非你并排打开两个应用,否则 iPad 始终处于横向模式。若要使用此模块锁定屏幕方向,需要通过在应用配置中将 ios.requireFullScreen 设置为 true,禁用对此功能的支持。

在 iOS 27 及更高版本中,requireFullScreen 不再阻止应用调整大小,因此无法可靠地锁定屏幕方向。请参阅 iOS 27 及更高版本中的方向锁定。有关 分屏视图 模式的更多信息,请参阅 Apple 官方文档。

在应用配置中配置

如果项目中使用配置插件(Continuous Native Generation (CNG)),可以使用内置的配置插件配置 expo-screen-orientation。该插件允许你配置各种无法在运行时设置、且需要重新构建应用二进制文件才能生效的属性。如果应用不使用 CNG,则需要手动配置此库。

Example app.json with config plugin

app.json
{ "expo": { "ios": { "requireFullScreen": true }, "plugins": [ [ "expo-screen-orientation", { "initialOrientation": "DEFAULT" } ] ] } }

Configurable properties

NameDefaultDescription
initialOrientationundefined
Only for: 
iOS

Sets the iOS initial screen orientation. Possible values: DEFAULT, ALL, PORTRAIT, PORTRAIT_UP, PORTRAIT_DOWN, LANDSCAPE, LANDSCAPE_LEFT, LANDSCAPE_RIGHT

Are you using this library in an existing React Native app?
  1. 在 Xcode 中使用 xed ios 打开 ios 目录。如果该目录不存在,请运行 npx expo prebuild -p ios 生成。
  2. 在 Xcode 中勾选 Requires Full Screen 复选框。它应位于 Project Target > General > Deployment Info 下。

使用 Expo Router 设置每个屏幕的方向

如果使用 Expo Router,可以通过 Stack.Screen 上的 orientation 选项为每个屏幕设置方向。此功能由 react-native-screens 提供支持,也是堆栈导航器中为每个屏幕设置方向的推荐方法。

src/app/_layout.tsx
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

ScreenOrientation.getOrientationAsync()

Android
iOS
Web

Gets the current screen orientation.

Returns a promise that fulfils with an Orientation value that reflects the current screen orientation.

ScreenOrientation.getOrientationLockAsync()

Android
iOS
Web

Gets the current screen orientation lock type.

Returns a promise which fulfils with an OrientationLock value.

ScreenOrientation.getPlatformOrientationLockAsync()

Android
iOS
Web

Gets the platform specific screen orientation lock type.

Returns a promise which fulfils with a PlatformOrientationInfo value.

ScreenOrientation.lockAsync(orientationLock)

Android
iOS
Web
ParameterTypeDescription
orientationLockOrientationLock

The orientation lock to apply. See the OrientationLock enum for possible values.


Lock the screen orientation to a particular OrientationLock.

Returns:
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); }

ScreenOrientation.lockPlatformAsync(options)

Android
iOS
Web
ParameterTypeDescription
optionsPlatformOrientationInfo

The platform specific lock to apply. See the PlatformOrientationInfo object type for the different platform formats.


Returns:
Promise<void>

Returns a promise with void value, resolving when the orientation is set and rejecting if an invalid option or value is passed.

ScreenOrientation.supportsOrientationLockAsync(orientationLock)

Android
iOS
Web
ParameterType
orientationLockOrientationLock

Returns whether the OrientationLock policy is supported on the device.

Returns:
Promise<boolean>

Returns a promise that resolves to a boolean value that reflects whether or not the orientationLock is supported.

ScreenOrientation.unlockAsync()

Android
iOS
Web

Sets the screen orientation back to the OrientationLock.DEFAULT policy.

Returns:
Promise<void>

Returns a promise with void value, which fulfils when the orientation is set.

Event subscriptions

ScreenOrientation.addOrientationChangeListener(listener)

Android
iOS
Web
ParameterTypeDescription
listenerOrientationChangeListener

Each orientation update will pass an object with the new OrientationChangeEvent to the listener.


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.

Returns:
EventSubscription

ScreenOrientation.removeOrientationChangeListener(subscription)

Android
iOS
Web
ParameterTypeDescription
subscriptionEventSubscription

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.

Returns:
void

ScreenOrientation.removeOrientationChangeListeners()

Android
iOS
Web

Removes all listeners subscribed to orientation change updates.

Returns:
void

Interfaces

Subscription

Android
iOS
Web

A subscription object that allows to conveniently remove an event listener from the emitter.

Subscription Methods

remove()

Android
iOS
Web

Removes an event listener for which the subscription has been created. After calling this function, the listener will no longer receive any events from the emitter.

Returns:
void

Types

OrientationChangeEvent

Android
iOS
Web
PropertyTypeDescription
orientationInfoScreenOrientationInfo

The current ScreenOrientationInfo of the device.

orientationLockOrientationLock

The current OrientationLock of the device.

OrientationChangeListener(event)

Android
iOS
Web
ParameterType
eventOrientationChangeEvent
Returns:

void

PlatformOrientationInfo

Android
iOS
Web
PropertyTypeDescription
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, -1 should be passed in.

screenOrientationLockWeb(optional)WebOrientationLock
Only for: 
Web

A web orientation lock to apply in the browser.

ScreenOrientationInfo

Android
iOS
Web
PropertyTypeDescription
horizontalSizeClass(optional)SizeClassIOS
Only for: 
iOS

The horizontal size class of the device.

orientationOrientation

The current orientation of the device.

verticalSizeClass(optional)SizeClassIOS
Only for: 
iOS

The vertical size class of the device.

Enums

Orientation

Android
iOS
Web

UNKNOWN

Orientation.UNKNOWN = 0

An unknown screen orientation. For example, the device is flat, perhaps on a table.

PORTRAIT_UP

Orientation.PORTRAIT_UP = 1

Right-side up portrait interface orientation.

PORTRAIT_DOWN

Orientation.PORTRAIT_DOWN = 2

Upside down portrait interface orientation.

LANDSCAPE_LEFT

Orientation.LANDSCAPE_LEFT = 3

Left landscape interface orientation.

LANDSCAPE_RIGHT

Orientation.LANDSCAPE_RIGHT = 4

Right landscape interface orientation.

OrientationLock

Android
iOS
Web

An enum whose values can be passed to the lockAsync method.

DEFAULT

OrientationLock.DEFAULT = 0

The default orientation. On iOS, this will allow all orientations except Orientation.PORTRAIT_DOWN. On Android, this lets the system decide the best orientation.

ALL

OrientationLock.ALL = 1

All four possible orientations

PORTRAIT

OrientationLock.PORTRAIT = 2

Any portrait orientation.

PORTRAIT_UP

OrientationLock.PORTRAIT_UP = 3

Right-side up portrait only.

PORTRAIT_DOWN

OrientationLock.PORTRAIT_DOWN = 4

Upside down portrait only.

LANDSCAPE

OrientationLock.LANDSCAPE = 5

Any landscape orientation.

LANDSCAPE_LEFT

OrientationLock.LANDSCAPE_LEFT = 6

Left landscape only.

LANDSCAPE_RIGHT

OrientationLock.LANDSCAPE_RIGHT = 7

Right landscape only.

OTHER

OrientationLock.OTHER = 8

A platform specific orientation. This is not a valid policy that can be applied in lockAsync.

UNKNOWN

OrientationLock.UNKNOWN = 9

An unknown screen orientation lock. This is not a valid policy that can be applied in lockAsync.

SizeClassIOS

Android
iOS
Web

Each iOS device has a default set of size classes that you can use as a guide when designing your interface.

UNKNOWN

SizeClassIOS.UNKNOWN = 0

COMPACT

SizeClassIOS.COMPACT = 1

REGULAR

SizeClassIOS.REGULAR = 2

WebOrientation

Android
iOS
Web

LANDSCAPE_PRIMARY

WebOrientation.LANDSCAPE_PRIMARY = "landscape-primary"

LANDSCAPE_SECONDARY

WebOrientation.LANDSCAPE_SECONDARY = "landscape-secondary"

PORTRAIT_PRIMARY

WebOrientation.PORTRAIT_PRIMARY = "portrait-primary"

PORTRAIT_SECONDARY

WebOrientation.PORTRAIT_SECONDARY = "portrait-secondary"

WebOrientationLock

Android
iOS
Web

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.

ANY

WebOrientationLock.ANY = "any"

LANDSCAPE

WebOrientationLock.LANDSCAPE = "landscape"

LANDSCAPE_PRIMARY

WebOrientationLock.LANDSCAPE_PRIMARY = "landscape-primary"

LANDSCAPE_SECONDARY

WebOrientationLock.LANDSCAPE_SECONDARY = "landscape-secondary"

NATURAL

WebOrientationLock.NATURAL = "natural"

PORTRAIT

WebOrientationLock.PORTRAIT = "portrait"

PORTRAIT_PRIMARY

WebOrientationLock.PORTRAIT_PRIMARY = "portrait-primary"

PORTRAIT_SECONDARY

WebOrientationLock.PORTRAIT_SECONDARY = "portrait-secondary"

UNKNOWN

WebOrientationLock.UNKNOWN = "unknown"