Ionic Screen Orientation Cordova Plugin在iOS环境下不会锁定到竖屏模式



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

我试着把下一行放到config.xml

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

android有效,对ios无效。有什么建议吗?

我希望我没有迟到。

你可以很容易地做到这一点,但使用不同的Cordova插件。

首先你需要使用Cordova . net. youik . Cordova .plugins.screenorientation plugin.

你可以很容易地编程锁定/解锁屏幕方向使用它。

// 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。在同一个文件中找到:" function 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。离子构建,离子帽复制,离子帽同步,问题修复!

最新更新