Ad

Building Ionic 4 Splash Screen, Didn't Do Anything (doesn't Work)

- 1 answer

I am having trouble making splash screen in ionic 4. It doesn't do anything, looking for answers especially in app.component.ts. There is nothing showing up in ionic 4 on how to make a splash screen. I tried using this.hide and this.show still doesn't work if anyone can help figure out how to make a splash screen in ionic 4.

Splash.ts

import { ModalController } from '@ionic/angular';
import { Component, OnInit } from '@angular/core';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';



@Component({
selector: 'app-splash',
templateUrl: './splash.page.html',
styleUrls: ['./splash.page.scss'],
})
export class SplashPage implements OnInit {


  constructor( public splashScreen: SplashScreen, public modalCtrl: ModalController) {

  }

  ionViewDidEnter() {

    this.splashScreen.hide();

    setTimeout(() => {
      this.modalCtrl.dismiss();
    }, 4000);

  }

  ngOnInit() {

  }

}

app.component.ts

import { Tab1Page } from './tab1/tab1.page';
import { SplashPage } from './splash/splash.page';
import { Component } from '@angular/core';

import { Platform, ModalController } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';


@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})
export class AppComponent {

    rootPage:any = SplashPage;

    constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, 
public modalCtrl: ModalController) {

        platform.ready().then(async () => {

            statusBar.styleDefault();

              const splash = this.modalCtrl.create({component:SplashPage,
              componentProps:{splash: SplashPage}});
               (await splash).present();

        });

    }

}

Splash.html

<ion-header>

</ion-header>


<ion-content>
 <div class="sk-folding-cube">
  <div class="sk-cube1 sk-cube"></div>
  <div class="sk-cube2 sk-cube"></div>
  <div class="sk-cube4 sk-cube"></div>
  <div class="sk-cube3 sk-cube"></div>
</div>
</ion-content>

Splash.css

.sk-folding-cube {
 margin: 20px auto;
 margin-top: 80%;
 width: 40px;
 height: 40px;
 position: relative;
 -webkit-transform: rotateZ(45deg);
        transform: rotateZ(45deg);
}

.sk-folding-cube .sk-cube {
  float: left;
  width: 50%;
  height: 50%;
  position: relative;
  -webkit-transform: scale(1.1);
      -ms-transform: scale(1.1);
          transform: scale(1.1); 
}
.sk-folding-cube .sk-cube:before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: white;
  -webkit-animation: sk-foldCubeAngle 2.4s infinite linear both;
          animation: sk-foldCubeAngle 2.4s infinite linear both;
  -webkit-transform-origin: 100% 100%;
      -ms-transform-origin: 100% 100%;
          transform-origin: 100% 100%;
} 
.sk-folding-cube .sk-cube2 {
  -webkit-transform: scale(1.1) rotateZ(90deg);
          transform: scale(1.1) rotateZ(90deg);
}
.sk-folding-cube .sk-cube3 {
  -webkit-transform: scale(1.1) rotateZ(180deg);
          transform: scale(1.1) rotateZ(180deg);
}
.sk-folding-cube .sk-cube4 {
  -webkit-transform: scale(1.1) rotateZ(270deg);
          transform: scale(1.1) rotateZ(270deg);
}
.sk-folding-cube .sk-cube2:before {
  -webkit-animation-delay: 0.3s;
          animation-delay: 0.3s;
}
 .sk-folding-cube .sk-cube3:before {
   -webkit-animation-delay: 0.6s;
           animation-delay: 0.6s; 
 }
 .sk-folding-cube .sk-cube4:before {
   -webkit-animation-delay: 0.9s;
           animation-delay: 0.9s;
 }
 @-webkit-keyframes sk-foldCubeAngle {
   0%, 10% {
     -webkit-transform: perspective(140px) rotateX(-180deg);
             transform: perspective(140px) rotateX(-180deg);
     opacity: 0; 
   } 25%, 75% {
     -webkit-transform: perspective(140px) rotateX(0deg);
             transform: perspective(140px) rotateX(0deg);
     opacity: 1; 
   } 90%, 100% {
     -webkit-transform: perspective(140px) rotateY(180deg);
             transform: perspective(140px) rotateY(180deg);
     opacity: 0; 
   } 
 }

 @keyframes sk-foldCubeAngle {
   0%, 10% {
     -webkit-transform: perspective(140px) rotateX(-180deg);
             transform: perspective(140px) rotateX(-180deg);
     opacity: 0; 
   } 25%, 75% {
     -webkit-transform: perspective(140px) rotateX(0deg);
             transform: perspective(140px) rotateX(0deg);
     opacity: 1; 
   } 90%, 100% {
     -webkit-transform: perspective(140px) rotateY(180deg);
             transform: perspective(140px) rotateY(180deg);
     opacity: 0; 
   }
 }

 ion-content {
   --background: rgb(57, 201, 194);
  }
Ad

Answer

First thing, update the config.xml file with the following lines.

<preference name="FadeSplashScreenDuration" value="300" />
<preference name="SplashScreenDelay" value="10000" />
<preference name="AutoHideSplashScreen" value="false" />
<preference name="FadeSplashScreen" value="true" />
<preference name="ShowSplashScreen" value="true" />

In the app.html, we just need a div that will act as an overlay for several seconds after the platform is ready.

<div *ngIf="showSplash" class="splash">
  <div class="spinner"></div>
</div>
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>

In the app.component.ts, we import an RxJS timer that will toggle the visibility of the overlay after 3000ms.

import { timer } from 'rxjs/observable/timer';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {

  // ...omitted

  showSplash = true; // <-- show animation

  initializeApp() {
    this.platform.ready().then(() => {

      this.statusBar.styleDefault();
      this.splashScreen.hide();  // <-- hide static image

      timer(3000).subscribe(() => this.showSplash = false) // <-- hide animation after 3s
    });
  }
}

Reference: https://angularfirebase.com/lessons/generate-a-custom-spash-screen-and-icons-in-ionic/

Ad
source: stackoverflow.com
Ad