How to restrict app to portrait mode only in ionic

2019-03-08 02:29发布

I'm working on Ionic/Cordova, I added this to androidManifest.xml but this didn't work for me and app is still showing in both ways

android:screenOrientation="portrait"

How can I restrict my app to portrait mode only? I have checked on android and it doesn't work

8条回答
Summer. ? 凉城
2楼-- · 2019-03-08 03:11

According to https://cordova.apache.org/docs/en/4.0.0/config_ref/#global-preferences,

Orientation allows you to lock orientation and prevent the interface from rotating in response to changes in orientation. Possible values are default, landscape, or portrait. Example:

<preference name="Orientation" value="landscape" />

Be aware that these values are case sensitive, it is "Orientation", not "orientation".

查看更多
小情绪 Triste *
3楼-- · 2019-03-08 03:14

If you want to do something on your orientation means you want to perform any action when change your app orientation then you have to use ionic framework plugin... https://ionicframework.com/docs/native/screen-orientation/

If you just want to restrict your app on portrait or landscape mode then you have to just add these following line in your config.xml

<preference name="orientation" value="portrait" />
                 OR
<preference name="orientation" value="landscape" />
查看更多
太酷不给撩
4楼-- · 2019-03-08 03:19

Inside your angular app.js add the line screen.lockOrientation('portrait'); like shown bellow:

an

angular.module('app', ['ionic'])
.run(function($ionicPlatform) {
    // LOCK ORIENTATION TO PORTRAIT.
    screen.lockOrientation('portrait');
  });
})

You will also have other code in the .run function, but the one you are interested in is the line I wrote. I haven't added the line <preference name="orientation" value="portrait" /> in my code, but you may need to add the cordova-plugin-device-orientation

查看更多
Fickle 薄情
5楼-- · 2019-03-08 03:19

In config.xml add the following line

<preference name="orientation" value="portrait" />
查看更多
Emotional °昔
6楼-- · 2019-03-08 03:26

You can try this:-

Cordova plugin to set/lock the screen orientation in a common way for iOS, Android, WP8 and Blackberry 10. This plugin is based on an early version of Screen Orientation API so the api does not currently match the current spec.

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

or try this code in xml config:-

<preference name="orientation" value="portrait" />
查看更多
看我几分像从前
7楼-- · 2019-03-08 03:30

First, you need to add the Cordova Screen Orientation Plugin using

cordova plugin add cordova-plugin-screen-orientation

and then add screen.lockOrientation('portrait'); to .run() method

angular.module('myApp', [])
.run(function($ionicPlatform) {

    screen.lockOrientation('portrait');
    console.log('Orientation is ' + screen.orientation);

});
})
查看更多
登录 后发表回答