react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[Initialization] Launch screen white flash

Open alinz opened this issue 11 years ago • 138 comments

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 avatar May 26 '15 04:05 alinz

Ok. digging deeper it seems the flash maybe coz my app renders nothing until a value is loaded from asyncstore.

render() {
  if(!this.state.loaded) return (null);
  return <View ...../>;
}

I think the loadingView thinks that the js finished rendering and hides the loadingView. However js just finished rendering with that return null

@nicklockwood @alinz I'm just shooting in the air. may not be the actual reason. What do you guys think.

Also one thing i noticed is that if i set those loadingViewFadeDelay etc values to 0.30 or higher, the transition is very very smooth. the defaults are 0.25 which is also smooth.

chirag04 avatar Jun 26 '15 19:06 chirag04

The purpose of the loadingFadeDelay is precisely to cover the scenario where your app doesn't render anything up immediately, so you're using it correctly.

You might want to consider setting the loadingViewFadeDelay explicitly though, in case the default ever changes - and make sure you test on the slowest device you support, in case it takes longer to load.

nicklockwood avatar Jun 26 '15 19:06 nicklockwood

I like @nicklockwood solution but you have to consider network latency which can not be predict.

One other solution is, set component state to some sort of default value to tell your component to display some sort of loading dialog.

alinz avatar Jun 26 '15 19:06 alinz

Cool. I think i have the solution.

What do you guys think about having 3 components all being the same.

1) launchScreen.xib
2) loadingView
3) return null in render to actually render a view which is like loadingView and launchScreen.xib?

Not sure how point 3 will scale on different devices though. Would you guys even suggest that?

chirag04 avatar Jun 26 '15 20:06 chirag04

If the startup time depends on network then tes, I'd definitely advocate drawing an "empty" view immediately instead of hoping it downloads faster than fade delay.

nicklockwood avatar Jun 26 '15 20:06 nicklockwood

depends on asyncstorage. no network

chirag04 avatar Jun 26 '15 20:06 chirag04

Whether you are depending on network or not, I don't really like using time base display. There is no way you can 100% guarantee that the time you set is accurate. You should always display something to user to indicate that something is happening but it is not ready to display. This is just a suggestion.

alinz avatar Jun 28 '15 19:06 alinz

Agreed. The purpose of the loadingView, like the standard iOS loading image, is just to to cover until your app is ready to display something, even if all the app is ready to display is a loading spinner.

That said, the reason I made it a view instead of an image is so you can make it non-static. You could add a UIActivityIndicatorView to the loadingView if you want.

nicklockwood avatar Jun 28 '15 20:06 nicklockwood

How can the launch image be positioned? I've tried adjusting the frame origin, but it always positions it in the center no matter what origin I used. I have a small top bar image that I want to position top and stretched from left to right.

This is what I have:

  UIImageView *launchView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LaunchImage"]];
  CGRect frame = launchView.frame;
  frame.origin.x = 0;
  frame.origin.y = 0;
  launchView.frame = frame;
  rootView.loadingView = launchView;
  rootView.loadingViewFadeDelay = 0.0;
  rootView.loadingViewFadeDuration = 0.0;

Intellicode avatar Jul 27 '15 09:07 Intellicode

It's not a launch image, it's a launch view, so you can position your imageView inside another container view and use the container as the launch view:

UIView *launchView = [[UIView alloc] init];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LaunchImage"]];
imageView.frame = CGRectMake(whatever);
[launchView addSubview:imageView];
...

nicklockwood avatar Jul 27 '15 11:07 nicklockwood

True, I suppose I could make the wrapper fill the screen space and position the image at the top. The wrapper will be automatically positioned in the center by the RootView it seems https://github.com/facebook/react-native/blob/757d6d204ae2d743634af64e3f78a4aad9d53f70/React/Base/RCTRootView.m#L191.

Intellicode avatar Jul 28 '15 07:07 Intellicode

@Intellicode yes.

nicklockwood avatar Jul 28 '15 08:07 nicklockwood

Just broke my app in production on iphone 4s using @dvine-multimedia's code. fix is to just put a break in the if.

chirag04 avatar Jul 29 '15 20:07 chirag04

One more crash because containsString is not available on ios 7.

@nicklockwood wondering if loading view will automatically pick the best resolution possible?

chirag04 avatar Jul 30 '15 15:07 chirag04

loadingView doesn't choose an image at all, it just uses what you pass in. The snippet I posted above for finding the launch inage will select the correct resolution for the device though.

nicklockwood avatar Jul 30 '15 19:07 nicklockwood

Your snippet is not picking the one with right dimensions though. any fix for that?

chirag04 avatar Jul 30 '15 19:07 chirag04

Were you using my solution or dvine's? What iPhone model are you using? Portrait or landscape?

nicklockwood avatar Jul 30 '15 20:07 nicklockwood

when using dvine's it picks up the right dimension. With ur solution it is having black space around the image. I assume the black space is coz it picks up the the image with wrong dimension.

I'm on iphone 6. portrait.

chirag04 avatar Jul 30 '15 20:07 chirag04

This is how it looks. Notice the white background.

Using the following code:

UIImageView *launchView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LaunchImage"]];
  rootView.loadingView = launchView;
  rootView.loadingViewFadeDelay = 0.20;
  rootView.loadingViewFadeDuration = 0.20;

lrn_launch_screen

chirag04 avatar Jul 30 '15 20:07 chirag04

That wasn't the solution I posted, this was:

// 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;
}

nicklockwood avatar Jul 30 '15 20:07 nicklockwood