Mohammed Azhar
Mohammed Azhar

Reputation: 259

Ionic 3 - Screen Orientation Lock not working

My requirement is to have the app fixed only to portrait mode on phones and only to landscape mode on tabs, starting with the splashscreen. Tried with the Screen Orientation plugin available on ionic native but did not work for me.

Things I have tried:

On app.component.ts:

constructor(..., private screenOrientation: ScreenOrientation) {
    platform.ready().then(() => {

        this.screenOrientation.lock('landscape');
        ScreenOrientation.lockOrientation('landscape');
        screen.msLockOrientation('landscape');
    this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE);

I have tried all of the above lines on home.ts too but they didn't work. When I set:

console.log(this.screenOrientation.type); 

This will print out the screen orientation correctly in logs. Whenever I set the subscribe method, that too works and alerts me whenever the screen orientation changes, which means that all the import statements are working perfectly and that I am able to access the plugin methods correctly. I am able to replicate this issue on both Android and iOS. Any help would be greatly appreciated.

Platform/library/dependency details:

"@angular/common": "4.1.3",

"ionic-angular": "3.6.0",

"ionic": "3.7.0"

Upvotes: 6

Views: 6498

Answers (2)

Jakegarbo
Jakegarbo

Reputation: 1301

In your config.xml you can try to add this line to make your orientation to lock on landscape mode.

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

Upvotes: 4

deanwilliammills
deanwilliammills

Reputation: 2787

The last option is suppose to work. I am using it the following way and it works:

For tablet: this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE);

For mobile: this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT);

Try to remove the ScreenOrientation module, remove the platform and then add the platform again and add the ScreenOrientation module again. It could also be the device you are debugging on

Upvotes: 0

Related Questions