【问题标题】:Ionic Screen Orientation Cordova Plugin is not locking to portrait mode in iOS environment离子屏幕方向 Cordova 插件在 iOS 环境中未锁定为纵向模式
【发布时间】:2015-07-10 08:29:23
【问题描述】:

我正在使用带有插件 cordova-plugin-screen-orientation 的 Ionic 框架,我想将我的应用程序方向锁定为始终为纵向。

我尝试通过将下一行放在config.xml 中来做到这一点

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

它适用于android,但不适用于ios。有什么建议吗?

【问题讨论】:

  • 你有ios和android屏幕方向插件吗?
  • 你不需要插件,卸载它,然后重试。您是否正在编辑根 www 文件夹?您是否从 CLI 运行应用程序?

标签: ios iphone cordova ionic screen-orientation


【解决方案1】:

希望我不会迟到。

您可以轻松做到这一点,但使用不同的 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/

【讨论】:

【解决方案2】:

我正在使用 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. 离子构建、离子上限复制、离子上限同步和问题已修复!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2021-09-17
    • 2011-10-21
    相关资源
    最近更新 更多