跳到主要内容
版本:v4

@capacitor/screen-orientation

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

安装

npm install @capacitor/screen-orientation
npx cap sync

iOS

锁定屏幕方向仅适用于 Capacitor 视图控制器(View Controller),不适用于其他被呈现的视图控制器(例如 Browser 插件呈现的控制器)。 要同时锁定被呈现的视图控制器,可以将此代码添加到应用的 AppDelegate.swift 文件中:

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

iPad 方向锁定

默认情况下,iPad 允许多任务处理,其方向无法被锁定。如果您需要在 iPad 上锁定方向,请将 Requires Full Screen 选项设置为 YES,方法是将以下内容添加到 Info.plist

  <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


接口

ScreenOrientationResult

属性类型
typeOrientationType

OrientationLockOptions

属性类型
orientationOrientationLockType

PluginListenerHandle

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