How to restrict app to portrait mode only in ionic for all platforms?

后端 未结 9 636
暖寄归人
暖寄归人 2020-12-23 08:37

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:sc         


        
相关标签:
9条回答
  • 2020-12-23 09:10

    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" />
    
    0 讨论(0)
  • 2020-12-23 09:11

    Please add android:screenOrientation="portrait" in the androidManifest.xml file as an attribute of activity tag for android.

    <activity
            android:name="com.example.demo_spinner.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
    </activity>
    
    0 讨论(0)
  • 2020-12-23 09:24

    If you want to restrict to portrait mode only on all devices you should add this line to the config.xml in your projects root folder.

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

    After that, please rebuild the platform by typing below text in command line:

    ionic build
    
    0 讨论(0)
  • 2020-12-23 09: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" />
    
    0 讨论(0)
  • 2020-12-23 09:27

    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

    0 讨论(0)
  • 2020-12-23 09: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);
    
    });
    })
    
    0 讨论(0)
提交回复
热议问题