简体   繁体   English

Cordova iOS 在单页上将屏幕方向更改为横向

[英]Cordova iOS Change Screen Orientation To Landscape on a Single Page

I have an app developed in Cordova 3+ for iPhone.我有一个用 Cordova 3+ 为 iPhone 开发的应用程序。 currently the application is running fine.目前该应用程序运行良好。 I have also restricted the Landscape view of the current application, ie the application display only in portrait.我还限制了当前应用程序的横向视图,即应用程序仅以纵向显示。

App consists of a lot of description and a report page.应用程序由很多描述和一个报告页面组成。 What i want is to display all pages in portrait and display the reports page in Landscape.我想要的是以纵向显示所有页面并以横向显示报告页面。

Iam using Backbone.js + underscore.js framework.我正在使用 Backbone.js + underscore.js 框架。

Do anyone have any suggestion or solution for this?有没有人对此有任何建议或解决方案?

Thanks in advance!提前致谢!

Edit: What i want is to force that particular report page to display in Landscape view.编辑:我想要的是强制该特定报告页面在横向视图中显示。 and display all other page in portrait.并以纵向显示所有其他页面。

Edit: Programatically control screen orientation in iPhone for cordova 3+编辑:在 iPhone 中为cordova 3+ 以编程方式控制屏幕方向

However i couldn't find any solutions. 但是我找不到任何解决方案。 Atlast i achieved it using CSS. Atlast我使用CSS实现了它。

-ms-transform:rotate(-90deg); /* IE 9 */
-webkit-transform:rotate(-90deg); /* Chrome, Safari, Opera */
transform:rotate(-90deg); /* Standard syntax */

Anyway it not a perfect solution but it works. 无论如何它不是一个完美的解决方案,但它的工作

And you can programatically change a single page to landscape using native code and call it via using javascript. 您可以使用本机代码以编程方式将单个页面更改为横向,并使用javascript调用它。

Create a new js file as ScreenOrientation.js and insert the below code, 创建一个新的js文件作为ScreenOrientation.js并插入以下代码,

var ScreenOrientation = {
   //alert(orientation);
callScreenOrientationNative: function(success,fail,orientation) {
   return Cordova.exec( success, fail,
                       "ScreenOrientation",
                       "screenorientationFunction",
                       [orientation]);
}
};

and add the above file in index.html as below, 并在index.html中添加上述文件,如下所示,

<script type="text/javascript" src="ScreenOrientation.js"></script>

Add the below function in any added js file(in my project i have added a script.js file to add common functions), 在任何添加的js文件中添加以下函数(在我的项目中,我添加了一个script.js文件来添加常用函数),

function callScreenOrientation(orientation) {
   ScreenOrientation.callScreenOrientationNative(nativePluginResultHandler,nativePluginErrorHandler,orientation);
}

function nativePluginResultHandler (result) {
}

function nativePluginErrorHandler (error) {
}

In config.xml add the below under feature name, 在config.xml中添加以下功能名称,

<!-- Screen Orientation custom plugin to display reports page. -->
   <feature name="ScreenOrientation">
       <param name="ios-package" value="ScreenOrientation"/>
   </feature>

Under Plugins add (For cordova < 3.0), 在插件下添加(对于cordova <3.0),

<plugins>
    <plugin name="ScreenOrientation" value="ScreenOrientation" />
</plugins>

On Cordova projects > Plugins right click and select new file, then from iOS select Cocoa touch then Select objective-C Class and click next, in class name insert "ScreenOrientation" and in Subclass of "CDVPlugin" and click next and click create. 在Cordova项目>插件上右键单击并选择新文件,然后从iOS选择Cocoa touch然后选择objective-C Class并单击下一步,在类名称中插入“ScreenOrientation”并在“CDVPlugin”的子类中单击“下一步”,然后单击“创建”。

Enter the below in ScreenOrientation.h, 在ScreenOrientation.h中输入以下内容,

#import <Cordova/CDV.h>

@interface ScreenOrientation : CDVPlugin

- (void) screenorientationFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;

@end

Enter the below in ScreenOrientation.m, 在ScreenOrientation.m中输入以下内容,

#import "ScreenOrientation.h"

@implementation ScreenOrientation

- (void) screenorientationFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
   [arguments pop];

   NSString *orientation = [arguments objectAtIndex:0];

   if ( [orientation isEqualToString:@"LandscapeLeft"] ) {
       NSLog(@"Landscape Left");
        [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeLeft];
   }
   else if ( [orientation isEqualToString:@"LandscapeRight"] ) {
       NSLog(@"Landscape Right");
       [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight];
   }
   else if ( [orientation isEqualToString:@"Portrait"] ) {
       NSLog(@"Portrait");
       [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait];
   }
   else if ( [orientation isEqualToString:@"PortraitUpsideDown"] ) {
       NSLog(@"Portrait upSide Down Left");
       [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortraitUpsideDown];
   }
}

@end

In Cordova project click search and search for "shouldAutoRotate and find the below and change the return, by default it will be 'YES' change it to 'NO'. 在Cordova项目中单击搜索并搜索“shouldAutoRotate并找到以下内容并更改返回,默认情况下将为'YES'将其更改为'NO'。

CDVViewController.m CDVViewController.m

- (BOOL)shouldAutorotate
{
    return NO;
}

And in project settings, in Device orientations check all options (not important though), 在项目设置中,在设备方向中检查所有选项(尽管不重要),

Project Settings > Device orientation > Tick all 4 options.

and call it from your project like this, 并从你的项目中调用它,

callScreenOrientation('LandscapeLeft');
callScreenOrientation('LandscapeRight');
callScreenOrientation('Portrait');
callScreenOrientation('PortraitUpsideDown');

This plugin allows you to programmatically rotate the screen on iOS and Android. 此插件允许您以编程方式在iOS和Android上旋转屏幕。

https://github.com/kant2002/cordova-plugin-screen-orientation https://github.com/kant2002/cordova-plugin-screen-orientation

Android rotation has visual glitches, due to performance (you mileage could vary depends on DOM size), but on rotation iOS is perfect. Android旋转有视觉故障,由于性能(你的里程可能会有所不同取决于DOM大小),但在轮换iOS是完美的。

Official Cordova Plugin 官方Cordova插件

According to this issue , even the official Cordova plugin ( cordova-plugin-screen-orientation ) for this has a fail point (that at the moment does not seem fixable). 根据这个问题 ,即使是官方的Cordova插件( cordova-plugin-screen-orientation )也有一个失败点(目前似乎无法修复)。 This is probably the route to go for the long-term, though. 不过,这可能是长期走的路。

Install the plugin and then use this JS: 安装插件然后使用这个JS:

screen.orientation.lock('landscape');

Or.... 要么....

CSS Only 仅限CSS

You can use CSS to fix this if you combine it with media queries, but it has a big snag, too. 如果将它与媒体查询结合使用,可以使用CSS来修复此问题,但它也有很大的障碍。 It won't affect where keyboards open, and keyboards opening may make the media query change: 它不会影响键盘打开的位置,键盘打开可能会使媒体查询发生变化:

Opening the soft keyboard on many devices in portrait orientation will cause the viewport to become wider than it is tall, thereby causing the browser to use landscape styles instead of portrait. 在纵向方向上打开许多设备上的软键盘将导致视口变得比它高,从而导致浏览器使用横向样式而不是纵向。 [ MDN docs ] [ MDN docs ]

If this does not matter for you page, you can simply add something like a class named orientation-landscape (or insert with JS if you cannot hard-code it) and then rotate it when the media query says it's portrait. 如果这对你的页面无关紧要,你可以简单地添加一个名为orientation-landscape的类(或者如果你不能硬编码就插入JS),然后在媒体查询说它是肖像时旋转它。

 @media (orientation: portrait) { .orientation-landscape { -webkit-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg); } } @media (orientation: landscape) { .orientation-portrait { -webkit-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg); } } /* just to show the change */ .orientation-landscape { background: blue; color: white; padding: 1em; text-align: center; } .orientation-portrait { border: 1px solid green; color: green; padding: 1em; background: lightgrey; text-align: center; } 
 <div class="orientation-portrait"> heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo </div> <div class="orientation-landscape"> heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo heyo </div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM