[英]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.