ionic2/3 禁止屏幕旋转,禁止横屏,竖屏

时间:2024-11-27 18:35:09

ionic2/ionic3禁止屏幕旋转,及解除禁止旋转

1.添加插件:

cmd到项目目录--->

cordova plugin add cordova-plugin-screen-orientation

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

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
declare var screen :any; //定义全局变量
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = TabsPage; constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
screen.orientation.lock('portrait-primary');
// //锁定到主竖屏
// screen.orientation.lock('portrait-primary');
// // //只禁止横屏
// screen.orientation.lock('landscape');
// // //只禁止竖屏
// screen.orientation.lock('portrait');
// // //锁定到副竖屏
// // screen.orientation.lock('portrait-secondary');
// // //锁定到主横屏
// // screen.orientation.lock('landscape-primary');
// // //锁定到副横屏
// // screen.orientation.lock('landscape-secondary');
// // //解除屏幕锁定
// // screen.orientation.unlock();
});
}
}

  总结:

这几个方法组合使用可以做:游戏界面旋转,视频的旋转与锁定