@capacitor/screen-orientation
Screen Orientation API 提供与屏幕方向相关的信息和功能。
安装
npm install @capacitor/screen-orientation
npx cap sync
iOS
锁定屏幕方向仅对 Capacitor View Controller 有效,但对其他正在呈现的 View Controller(例如 Browser 插件呈现的 View Controller)无效。
要同时锁定已呈现的 View Controller,可以将以下代码添加到应用的 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>
锁定屏幕方向。
从 Android targetSdk 36 开始,此方法在 Android 16 及以上版本中对大屏幕(如平板电脑)无效。
您可以通过在 AndroidManifest.xml 的 <application> 或 <activity> 中添加 <property android:name="android.window.PROPERTY_COMPAT_ALLOW_RESTRICTED_RESIZABILITY" android:value="true" /> 来选择退出此行为。
但请注意,此选择退出是临时的,在 Android 17 中将不再有效。Android 不鼓励为大屏幕设置特定方向。
普通 Android 手机不受此变化影响。
更多信息请查看 Android 文档 https://developer.android.com/about/versions/16/behavior-changes-16#adaptive-layouts
| 参数 | 类型 |
|---|---|
options | |
始于: 4.0.0
unlock()
unlock() => Promise<void>
解锁屏幕方向。
始于: 4.0.0
addListener('screenOrientationChange', ...)
addListener(eventName: 'screenOrientationChange', listenerFunc: (orientation: ScreenOrientationResult) => void) => Promise<PluginListenerHandle>
监听屏幕方向变化。
| 参数 | 类型 |
|---|---|
eventName | 'screenOrientationChange' |
listenerFunc | |
返回:
Promise<PluginListenerHandle>
始于: 4.0.0
removeAllListeners()
removeAllListeners() => Promise<void>
移除所有监听器。
始于: 4.0.0
接口
ScreenOrientationResult
| 属性 | 类型 |
|---|---|
type | OrientationType |
OrientationLockOptions
| 属性 | 类型 | 描述 |
|---|---|---|
orientation | | 注意:TypeScript v5.2+ 用户应从 @capacitor/screen-orientation 导入 OrientationLockType。 |
PluginListenerHandle
| 属性 | 类型 |
|---|---|
remove | () => Promise<void> |
类型别名
OrientationLockType
'any' | 'natural' | 'landscape' | 'portrait' | 'portrait-primary' | 'portrait-secondary' | 'landscape-primary' | 'landscape-secondary'