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

后端 未结 9 637
暖寄归人
暖寄归人 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:33

    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".

    0 讨论(0)
  • 2020-12-23 09:33

    In config.xml add the following line

    <preference name="orientation" value="portrait" />
    
    0 讨论(0)
  • 2020-12-23 09:34

    Ionic v2+ Update

    For Ionic versions 2 and later, you will need to also install the corresponding Ionic Native package for any plugin you use, including cordova-plugin- and phonegap-plugin- plugins.

    1. Install Ionic Native Plugin

      Install Ionic Native's TypeScript module for the plugin from the CLI.

      $ ionic cordova plugin add --save cordova-plugin-screen-orientation
      $ npm install --save @ionic-native/screen-orientation
      

      * Note that the --save command is optional and can be omitted if you do not wish to add the plugin to your package.json file

    2. Import ScreenOrientation Plugin

      Import plugin into your controller, more details available in the documentation.

      import { ScreenOrientation } from '@ionic-native/screen-orientation';
      
      @Component({
          templateUrl: 'app.html',
          providers: [
              ScreenOrientation
          ]
      })
      
      constructor(private screenOrientation: ScreenOrientation) {
        // Your code here...
      }
      
    3. Do Your Thing

      Lock and unlock the screen orientation programatically.

      // Set orientation to portrait
      this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT);
      
      // Disable orientation lock
      this.screenOrientation.unlock();
      
    4. Bonus Points

      You can also get the current orientation.

      // Get current orientation
      console.log(this.screenOrientation.type);  // Will return landscape" or "portrait"
      
    0 讨论(0)
提交回复
热议问题