简体   繁体   English

离子屏幕方向 Cordova 插件在 iOS 环境中未锁定为纵向模式

[英]Ionic Screen Orientation Cordova Plugin is not locking to portrait mode in iOS environment

I'm using Ionic framework with the plugin cordova-plugin-screen-orientation and I would like to lock my app orientation to be always portrait.我正在使用带有插件cordova-plugin-screen-orientation Ionic 框架,我想将我的应用程序方向锁定为始终为纵向。

I tried to do it by putting the next line in the config.xml我试图通过将下一行放在config.xml中来做到这一点

<preference name="orientation" value="portrait" />

It works on android but not on ios .它适用于android但不适用于ios Any suggestions?有什么建议?

I hope I'm not late. 我希望我不迟到。

You can do it easily but with a different Cordova plugin. 您可以轻松完成此操作,但可以使用其他Cordova插件。

First you will need to use Cordova net.yoik.cordova.plugins.screenorientation plugin. 首先,您将需要使用Cordova net.yoik.cordova.plugins.screenorientation插件。

You can easily programmatically lock/unlock screen orientation using it. 您可以使用它轻松地以编程方式锁定/解锁屏幕方向。

// set to either landscape
screen.lockOrientation('landscape');

// allow user rotate
screen.unlockOrientation();

If you want to do this for all pages/views do it like this: 如果要对所有页面/视图执行此操作,请按以下步骤操作:

$ionicPlatform.ready(function() {
   screen.lockOrientation('landscape');
});

Next, to do it only on selected pages, use this code inside an appropriate page controller: 接下来,仅在选定的页面上执行此操作,请在适当的页面控制器内使用以下代码:

$scope.$on('$ionicView.beforeEnter', function(){
    screen.lockOrientation('landscape');
});

If executed in a right controller this code will trigger orientation lock before the view becomes visible. 如果在正确的控制器中执行,此代码将在视图变为可见之前触发方向锁定。

Read this if you want to find out more: http://www.gajotres.net/changing-locking-screen-orientation-in-ionic-application/ 如果您想了解更多信息, 阅读以下内容: http : //www.gajotres.net/changing-locking-screen-orientation-in-ionic-application/

I am using Ionic/Capacitor and Vuejs, I also had the same problem with the plugin for iOS.我正在使用 Ionic/Capacitor 和 Vuejs,iOS 插件也有同样的问题。 This is what I did and it solved the problem.这就是我所做的,它解决了问题。

To fix the bug to be able to lock the screen to the specified orientation on iOS:要修复错误以在 iOS 上将屏幕锁定到指定方向:

1. Open AppDelegate.swift for your app. 1.为您的应用打开 AppDelegate.swift。 You can find this file inside ios/App/App/AppDelegate.swift你可以在 ios/App/App/AppDelegate.swift 中找到这个文件

2. Add the following code: 2.添加以下代码:

var orientationLock = UIInterfaceOrientationMask.all

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return self.orientationLock
}

@objc func setOrientationLock(_ notification: Notification)
{
    if let data = notification.userInfo as? [String: Int]
    {
        for (_, mask) in data
        {
            switch mask
            {
                case 1: self.orientationLock = UIInterfaceOrientationMask.portrait
                break;
                case 2: self.orientationLock = UIInterfaceOrientationMask.portraitUpsideDown
                break;
                case 3: self.orientationLock = UIInterfaceOrientationMask.landscapeRight
                break;
                case 4: self.orientationLock = UIInterfaceOrientationMask.landscapeLeft
                break;
                case 5: self.orientationLock = UIInterfaceOrientationMask.landscape
                break;
                default: self.orientationLock = UIInterfaceOrientationMask.all
            }
        }
    }
}

3. In the same file locate: "func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {" 3.在同一文件中找到:“func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {”

4. Add the following code inside the function before the "return true" statement: 4.在函数内“return true”语句之前添加以下代码:

NotificationCenter.default.addObserver(self, selector: #selector(self.setOrientationLock), name: NSNotification.Name(rawValue: "CAPOrientationLocked"), object: nil)

5. ionic build, ionic cap copy, ionic cap sync, and problem FIXED! 5.离子构建、离子上限复制、离子上限同步和问题已修复!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在视图中锁定纵向方向? IOS 7 - Locking Portrait Orientation in View? iOS 7 Ionic cordova-plugin-screen-orientation 在 Ios 上不再起作用 - Ionic cordova-plugin-screen-orientation doesn't work anymore on Ios MPMoviePlayerController在全屏模式下停止工作//纵向// iOS 7 - MPMoviePlayerController stops working in full screen mode // portrait orientation // iOS 7 cordova-plugin-screen-orientation不适用于ios中的webview插件 - cordova-plugin-screen-orientation is not working with webview plugin in ios 在 iOS 14 中锁定屏幕方向 - Locking screen orientation in iOS 14 Cordova屏幕方向未在iOS上将窗口调整为100%-ionic app - cordova screen orientation not resizing window to 100% on ios - ionic app 如果屏幕在纵向模式下被锁定,如何检测方向? - How to detect orientation, if screen is locked in portrait mode? 科尔多瓦屏幕方向插件不起作用 - Cordova screen orientation plugin not working Cordova:在iOS上屏幕方向从横向更改为纵向时,Inappbrowser中断了UI - Cordova : Inappbrowser breaks UI while Screen orientation changes from landscape to portrait on iOS IOS设备方向卡在纵向模式下,没有横向 - IOS Device Orientation Stuck at Portrait mode, no Landscape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM