如何强制加载离子3中的特定方向?

问题描述 投票:0回答:2

我尝试在设备和localhost中的ionViewDidLoad()中使用ScreenOrientation设置方向,但我无法强制进行更改。

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { ScreenOrientation } from '@ionic-native/screen-orientation';

@Component({
  selector: 'page-Test',
  templateUrl: 'Test.html'
})
export class TestPage {
  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    private screenOrientation: ScreenOrientation) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad TestPage');
    this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE_PRIMARY);
  }
}
ionic-framework ionic3
2个回答
0
投票

尝试:

ionViewDidLoad() {
    console.log('ionViewDidLoad TestPage');
    this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE);
  }

要么

ionViewDidLoad() {
    console.log('ionViewDidLoad TestPage');
    this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE_SECONDARY);
  }

0
投票

你必须在spash屏幕隐藏之前在app.components.ts文件initializeApp()方法中添加this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE_PRIMARY)行。

示例:app.components.ts

initializeApp() {
    this.platform.ready().then(async () => {
        this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE_PRIMARY).then(async ()=>{
            this.statusBar.styleDefault();
            this.splashScreen.hide();

            this.rootPage="HomePage";
            // rest of your code goes here
        });
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.