簡體   English   中英

離子屏幕方向 Cordova 插件在 iOS 環境中未鎖定為縱向模式

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

我正在使用帶有插件cordova-plugin-screen-orientation Ionic 框架,我想將我的應用程序方向鎖定為始終為縱向。

我試圖通過將下一行放在config.xml中來做到這一點

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

它適用於android但不適用於ios 有什么建議?

我希望我不遲到。

您可以輕松完成此操作,但可以使用其他Cordova插件。

首先,您將需要使用Cordova net.yoik.cordova.plugins.screenorientation插件。

您可以使用它輕松地以編程方式鎖定/解鎖屏幕方向。

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

// allow user rotate
screen.unlockOrientation();

如果要對所有頁面/視圖執行此操作,請按以下步驟操作:

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

接下來,僅在選定的頁面上執行此操作,請在適當的頁面控制器內使用以下代碼:

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

如果在正確的控制器中執行,此代碼將在視圖變為可見之前觸發方向鎖定。

如果您想了解更多信息, 閱讀以下內容: http : //www.gajotres.net/changing-locking-screen-orientation-in-ionic-application/

我正在使用 Ionic/Capacitor 和 Vuejs,iOS 插件也有同樣的問題。 這就是我所做的,它解決了問題。

要修復錯誤以在 iOS 上將屏幕鎖定到指定方向:

1.為您的應用打開 AppDelegate.swift。 你可以在 ios/App/App/AppDelegate.swift 中找到這個文件

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.在同一文件中找到:“func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {”

4.在函數內“return true”語句之前添加以下代碼:

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

5.離子構建、離子上限復制、離子上限同步和問題已修復!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM