[Initialization] Launch screen white flash
I just wondering if there is a good practice about LaunchScreen. The reason I'm asking this is, if one adds LaunchScreen, there is a white flash before react-native kicks in and load the app. Is there any way we can prevent this?
@alinz - this is fixed in the next release: https://github.com/tadeuzagallo/react-native/commit/8d992262ed2dcf0d46e8d22e605764902c33012f
@alinz does it look something like https://www.dropbox.com/s/xq522ywqsd16vqe/example.mov?dl=0
@liubko Yes, this is exactly what I want to get rid of. I haven't applied the patch yet but I will do it tonight.
@brentvatne: I don't see how that patch would fix this problem (if I'm blind/stupid please forgive :-). The problem is that there's a moment between when the app has launched and React is still working. Ideally the LaunchScreen would show until React is finished and fully rendered as it does in a "fully native" app.
@oblador - ah, this would allow you to transition more smoothly by having the same background colour as the launch screen in the "flicker" moment.
@vjeux - how do you get past this issue in FB apps? I noticed that F8, which if I understand correctly is 100% React Native, transitions directly from the launch screen to the app. cc @nicklockwood
There's a (separate) fix for this issue too. Use the new loadingView property of RCTRootView, and set it to a full-screen UIImageView showing your launch image. The code might look something like this:
UIImageView *launchView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MyLaunchImage"];
rootView.loadingView = launchView;
I've been experimenting with a way to automatically detect the launch image and make this completely automatic, but it's not really ready yet. If you're interested, it looks like this:
// TODO: support landscape orientation
// Get launch image
NSString *launchImageName = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGFloat height = MAX(RCTScreenSize().width, RCTScreenSize().height);
if (height == 480) launchImageName = @"[email protected]"; // iPhone 4/4s
else if (height == 568) launchImageName = @"[email protected]"; // iPhone 5/5s
else if (height == 667) launchImageName = @"[email protected]"; // iPhone 6
else if (height == 736) launchImageName = @"[email protected]"; // iPhone 6+
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CGFloat scale = RCTScreenScale();
if (scale == 1) launchImageName = @"LaunchImage-700-Portrait~ipad.png"; // iPad
else if (scale == 2) launchImageName = @"LaunchImage-700-Portrait@2x~ipad.png"; // Retina iPad
}
// Create loading view
UIImage *image = [UIImage imageNamed:launchImageName];
if (image) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:(CGRect){CGPointZero, RCTScreenSize()}];
imageView.contentMode = UIViewContentModeBottom;
imageView.image = image;
rootView.loadingView = imageView;
}
An easy solution to what @nicklockwood described is to load the launchscreen xib if you're on iOS 8+.
@ide I am using launchscreen xib. I see my lauchscreen view at the launch time but before my app boots up, I see a white view (flash).
@alinz, I believe @ide means to use your launchscreen xib to set the RCTRootView loadingView (which you'll still need to do manually). There is no automatic support for doing that (yet).
To clarify, iOS hides your launch screen at the point when React begins loading. To avoid seeing a blank screen, you'll need to extend the time that the launch screen is displayed by manually showing the same view as the RCTRootView's loadingView.
Thanks @nicklockwood for clarification. That was my bad. :)
Curtesy of http://stackoverflow.com/a/29115477/255765 I ended up with this:
for (NSString *imgName in allPngImageNames){
// Find launch images
if ([imgName containsString:@"LaunchImage"]){
UIImage *img = [UIImage imageNamed:imgName]; //-- this is a launch image
// Has image same scale and dimensions as our current device's screen?
if (img.scale == [UIScreen mainScreen].scale && CGSizeEqualToSize(img.size, [UIScreen mainScreen].bounds.size)) {
NSLog(@"Found launch image for current device %@", img.description);
UIImageView *launchView = [[UIImageView alloc] initWithImage: img];
rootView.loadingView = launchView;
}
}
}
Seems to work.
+1 for a standard/documented way.
@dvine-multimedia does that work better than the code snippet I provided above?
@myusuf3
@nicklockwood Actually they do very much the same thing. When I started to look into this I probably didn't understand what to do with your snipped for pure lack of any xcode experience. When I finally got to the point where I understood what to do, I didn't realize, that you already had been there. Beside that, I personally find "my" solution a bit more readable. But that pure aesthetics.
@nicklockwood I tried your code but i still see a white flash. I created a launch image to set as the loading view. launch image is same as the launchScreen.xib.
This is the only thing holding the release of our app. Really appreciate any help on this.
Is the flash appearing after the launch image is hidden, or does the launch image fail to appear? (This may be hard to determine just by looking - change the launch image to something like a solid red rectangle temporarily to be sure).
@nicklockwood I tried to change the color between xib file and the launchScreen Image. I was still seeing that flash.
My code looks like this:
UIImageView *launchView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"launchScreen1"]];
rootView.loadingView = launchView;
rootView.loadingViewFadeDelay = 0.0;
rootView.loadingViewFadeDuration = 0.15;
I removed the last two line loadingViewFadeDelay and duration now and it seems the problem is solved. I don't see any flash. Nice smooth transition.
@chirag04 I just tried it with sample project and I didn't see the flash :(
here's what I did
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"whiteFlash"
launchOptions:launchOptions];
//here's what I did
UIImage *image = [UIImage imageNamed:@"LaunchImage"];
if (image) {
UIImageView *launchView = [[UIImageView alloc] initWithImage: image];
launchView.contentMode = UIViewContentModeBottom;
launchView.image = image;
rootView.loadingView = launchView;
}
///////////////////////////
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [[UIViewController alloc] init];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;