我正在尝试获取platform
对象的屏幕高度和宽度,但显然屏幕方向混淆了这些值。
测试代码:
platform.ready().then(() => {
console.log("orientation", screenOrientation.type);
console.log("platform height", this.platform.height());
console.log("platform width", this.platform.width());
events.subscribe('orientation-changed', (screenOrientation) => {
console.log("orientation", screenOrientation.type);
console.log("platform height", this.platform.height());
console.log("platform width", this.platform.width());
});
});
我以纵向模式启动运行该应用程序,并得到:
orientation portrait-primary
platform height 567
platform width 360
我将手机旋转到横向,得到:
orientation landscape-primary
platform height 567
platform width 360
直到现在,一切似乎都井井有条。但是现在,如果我将手机调回纵向模式,我会得到:
orientation portrait-primary
platform height 335
platform width 598
如果我再次将其转换为横向:
orientation landscape-primary
platform height 567
platform width 360
如何在不担心手机方向的情况下获得正确的高度和宽度?
我遇到了同样的事情。我的解决方案是在组件构造函数中存储一次平台高度和宽度,然后在事件处理程序中使用这些值。
在构造函数中:
if(this._screenOrientation.type === this._screenOrientation.ORIENTATIONS.PORTRAIT
|| this._screenOrientation.type === this._screenOrientation.ORIENTATIONS.PORTRAIT_PRIMARY
|| this._screenOrientation.type === this._screenOrientation.ORIENTATIONS.PORTRAIT_SECONDARY){
this._pxDeviceWidth = this._platform.width();
this._pxDeviceHeight = this._platform.height();
}else{
this._pxDeviceWidth = this._platform.height();
this._pxDeviceHeight = this._platform.width();
}
然后,只需在事件处理程序函数中引用this._pxDeviceWidth
或this._pxDeviceHeight
。