Files
cocos4/port/native/engine/jsb-safearea.js
T
Knox 5362151608 add getSafeArea for android and iOS & improve safe area (#7863)
* add getSafeArea for android and iOS & improve safe area

* refine code
2020-11-25 16:09:06 +08:00

30 lines
1.1 KiB
JavaScript

let SafeArea = cc.SafeArea;
if (SafeArea) {
let _onEnable = SafeArea.prototype.onEnable;
let _onDisable = SafeArea.prototype.onDisable;
Object.assign(SafeArea.prototype, {
onEnable(){
_onEnable.call(this);
this._adaptSafeAreaChangeWithThis = this.adaptSafeAreaChange.bind(this);
this._updateAreaWithThis = this.adaptSafeAreaChange.bind(this);
window.addEventListener('orientationchange', this._adaptSafeAreaChangeWithThis);
window.addEventListener('safearea-change', this._updateAreaWithThis);
},
onDisable(){
_onDisable.call(this);
window.removeEventListener('orientationchange', this._adaptSafeAreaChangeWithThis);
window.removeEventListener('safearea-change', this._updateAreaWithThis);
},
adaptSafeAreaChange(){
if (JSB && (cc.sys.os === cc.sys.OS_IOS || cc.sys.os === cc.sys.OS_ANDROID)) {
setTimeout(() => {
this.updateArea();
}, 200);
}
}
});
}