I'm trying to get a background image to work on a multi-paged Ionic app that I'm updating from Ionic 3 to 5. I was getting the flickering background image problem on iOS for any page other than the first page loaded. I tried implementing this solution: How to put image in both <ion-header> and <ion-content> in Ionic and it works in the web browser but when I run it in the simulator in Xcode, ionViewWillEnter does not fire in iOS but will fire in the browser (live reload).
This is the old code in CSS that produces the flickering in iOS:
ion-content {
--background: url(/assets/imgs/bg/piano.jpg) no-repeat center/ cover fixed;
}
The workaround looks like this:
import { DomController } from '@ionic/angular';
...
constructor(public navCtrl: NavController, private domCtrl: DomController) {
}
...
private initializeBackground(): void {
try {
console.log ('initializing background');
const content = document.querySelector('#level');
const innerScroll = content.shadowRoot.querySelector('.inner-scroll');
this.domCtrl.write(() => {
innerScroll.setAttribute(
'style',
'background: url("/assets/imgs/bg/piano.jpg") no-repeat center center / cover'
);
});
console.log('background initialized'); } catch (e) {}
console.log('background not initialized');
}
...
ionViewWillEnter() {
console.log('After Init?');
this.initializeBackground();
}
When I run this, ionViewWillEnter fires when running it in the web browser but when I run it in the simulator, it does not. I've been reading that it will only trigger once and has problems when triggering from a side menu https://fantashit.com/ionic-4-ionviewwillenter-only-triggers-once/ But I'm not sure this is the problem I've got because it makes no mention of this working in the browser, but not in iOS.
All I want is to set different background images on different pages that work on all platforms. Surely there is a better way? Any help would be greatly appreciated.