Guest User

Untitled

a guest
Jul 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Platform } from 'ionic-angular';
  3.  
  4. const iPhoneX = { height: 2436, width: 1125 };
  5.  
  6. @Injectable()
  7. export class NotchService {
  8.  
  9. constructor(
  10. private platform: Platform,
  11. ) {}
  12.  
  13. init() {
  14. (<any>window).addEventListener('orientationchange', () => {
  15. console.log('orientationchange');
  16. this.onOrientationChanged();
  17. });
  18.  
  19. this.onOrientationChanged();
  20. }
  21.  
  22. private onOrientationChanged() {
  23. if (!this.platform.is('ios')) { return; }
  24.  
  25. const ratio = (<any>window).devicePixelRatio || 1;
  26. const screen = {
  27. width : (<any>window).screen.width * ratio,
  28. height : (<any>window).screen.height * ratio,
  29. };
  30.  
  31. const isiPhoneXPortrait = screen.width === iPhoneX.width && screen.height === iPhoneX.height;
  32. const isiPhoneXLandscape = screen.width === iPhoneX.height && screen.height === iPhoneX.width;
  33.  
  34. console.log(screen);
  35. console.log(`isiPhoneXPortrait = ${isiPhoneXPortrait}`);
  36. console.log(`isiPhoneXLandscape = ${isiPhoneXLandscape}`);
  37.  
  38.  
  39. if (isiPhoneXPortrait || isiPhoneXLandscape) {
  40. (<any>document).body.classList.add('iPhoneX');
  41. }
  42.  
  43. if (isiPhoneXPortrait) {
  44. (<any>document).body.classList.remove('landscape');
  45. (<any>document).body.classList.add('portrait');
  46. } else if (isiPhoneXLandscape) {
  47. (<any>document).body.classList.remove('portrait');
  48. (<any>document).body.classList.add('landscape');
  49. }
  50. }
  51. }
Add Comment
Please, Sign In to add comment