跳到主要内容
版本:v5

@capacitor/screen-orientation

屏幕方向 API 提供与屏幕方向相关的信息和功能。

安装

npm install @capacitor/screen-orientation@latest-5
npx cap sync

iOS 注意事项

屏幕方向锁定仅对 Capacitor 的视图控制器有效,但对其他呈现的视图控制器(如由浏览器插件呈现的控制器)无效。若要同时锁定呈现的视图控制器,可在应用的 AppDelegate.swift 文件中添加以下代码:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask(rawValue: (self.window!.rootViewController as! CAPBridgeViewController).supportedInterfaceOrientations.rawValue)
}

iPad 方向锁定

默认情况下,iPad 允许多任务处理且其方向无法锁定。如需在 iPad 上锁定方向,请在 Info.plist 中添加以下配置,将 Requires Full Screen 选项设为 YES

  <key>UIRequiresFullScreen</key>
<true/>

API 接口

orientation()

orientation() => Promise<ScreenOrientationResult>

获取当前屏幕方向。

返回值:

Promise<ScreenOrientationResult>

自版本: 4.0.0


lock(...)

lock(options: OrientationLockOptions) => Promise<void>

锁定屏幕方向。

参数类型
options
OrientationLockOptions

自版本: 4.0.0


unlock()

unlock() => Promise<void>

解除屏幕方向锁定。

自版本: 4.0.0


addListener('screenOrientationChange', ...)

addListener(eventName: 'screenOrientationChange', listenerFunc: (orientation: ScreenOrientationResult) => void) => Promise<PluginListenerHandle> & PluginListenerHandle

监听屏幕方向变化事件。

参数类型
eventName'screenOrientationChange'
listenerFunc
(orientation: ScreenOrientationResult) => void

返回值:

Promise<PluginListenerHandle> & PluginListenerHandle

自版本: 4.0.0


removeAllListeners()

removeAllListeners() => Promise<void>

移除所有事件监听器。

自版本: 4.0.0


Interfaces

ScreenOrientationResult

属性类型
typeOrientationType

OrientationLockOptions

属性类型说明
orientation
OrientationLockType
注意:TypeScript v5.2+ 用户应从 @capacitor/screen-orientation 导入 OrientationLockType 类型。

PluginListenerHandle

属性类型
remove() => Promise<void>

Type Aliases

OrientationLockType


'any' | 'natural' | 'landscape' | 'portrait' | 'portrait-primary' | 'portrait-secondary' | 'landscape-primary' |
'landscape-secondary'