README.md 8.94 KB
Newer Older
crazycodeboy's avatar
crazycodeboy committed
1
# react-native-splash-screen
crazycodeoby's avatar
crazycodeoby committed
2

Jia PengHui's avatar
Jia PengHui committed
3

crazycodeboy's avatar
crazycodeboy committed
4
[![Download](https://img.shields.io/badge/Download-v3.0.6-ff69b4.svg) ](https://www.npmjs.com/package/react-native-splash-screen)
crazycodeboy's avatar
update    
crazycodeboy committed
5
6
7
8
9
[ ![PRs Welcome](https://img.shields.io/badge/PRs-Welcome-brightgreen.svg)](https://github.com/crazycodeboy/react-native-splash-screen/pulls)
[ ![react-native-splash-screen release](https://img.shields.io/github/release/crazycodeboy/react-native-splash-screen.svg?maxAge=2592000?style=flat-square)](https://github.com/crazycodeboy/GitHubPopular/releases)
[ ![语言 中文](https://img.shields.io/badge/语言-中文-feb252.svg)](https://github.com/crazycodeboy/react-native-splash-screen/blob/master/README.zh.md)
[![License MIT](http://img.shields.io/badge/license-MIT-orange.svg?style=flat)](https://raw.githubusercontent.com/crazycodeboy/react-native-check-box/master/LICENSE)
[ ![原理 解析](https://img.shields.io/badge/原理-解析-brightgreen.svg)](https://github.com/crazycodeboy/RNStudyNotes/blob/master/React%20Native%20%E9%97%AE%E9%A2%98%E5%8F%8A%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%E5%90%88%E9%9B%86/React%20Native%20%E5%90%AF%E5%8A%A8%E7%99%BD%E5%B1%8F%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3%E6%95%99%E7%A8%8B/React%20Native%20%E5%90%AF%E5%8A%A8%E7%99%BD%E5%B1%8F%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3%E6%95%99%E7%A8%8B.md)
crazycodeoby's avatar
crazycodeoby committed
10

Jacob Lauritzen's avatar
Jacob Lauritzen committed
11
A splash screen API for react-native which can programatically hide and show the splash screen. Works on iOS and Android.
crazycodeboy's avatar
crazycodeboy committed
12
13
14
15

## Content

- [Installation](#installation)
crazycodeboy's avatar
crazycodeboy committed
16
- [Examples](#examples)
crazycodeboy's avatar
crazycodeboy committed
17
18
19
- [Getting started](#getting-started)
- [API](#api)
- [Contribution](#contribution)
crazycodeboy's avatar
crazycodeboy committed
20
21
22
- [Changes](#changes)

## Changes
crazycodeboy's avatar
crazycodeboy committed
23
For React Native >= 0.47.0 use [v3.+](https://github.com/crazycodeboy/react-native-splash-screen/releases), for React Native < 0.47.0 use [v2.1.0](https://github.com/crazycodeboy/react-native-splash-screen/releases/tag/v1.0.9)
crazycodeboy's avatar
crazycodeboy committed
24
25
26
27
28
29
30
31

## Examples  
* [Examples](https://github.com/crazycodeboy/react-native-splash-screen/tree/master/examples)

![react-native-splash-screen-Android](https://raw.githubusercontent.com/crazycodeboy/react-native-splash-screen/master/examples/Screenshots/react-native-splash-screen-Android.gif)
![react-native-splash-screen-iOS](https://raw.githubusercontent.com/crazycodeboy/react-native-splash-screen/master/examples/Screenshots/react-native-splash-screen-iOS.gif)


crazycodeboy's avatar
crazycodeboy committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

## Installation

### First step(Download):
Run `npm i react-native-splash-screen --save`

### Second step(Plugin Installation):

#### Automatic installation

`react-native link react-native-splash-screen` or `rnpm link react-native-splash-screen`

#### Manual installation  

**Android:**

Jacob Lauritzen's avatar
Jacob Lauritzen committed
48
1. In your android/settings.gradle file, make the following additions:
Akshay Kadam's avatar
Akshay Kadam committed
49
```java
crazycodeboy's avatar
crazycodeboy committed
50
51
52
53
include ':react-native-splash-screen'   
project(':react-native-splash-screen').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-splash-screen/android')
```

Jacob Lauritzen's avatar
Jacob Lauritzen committed
54
2. In your android/app/build.gradle file, add the `:react-native-splash-screen` project as a compile-time dependency:
crazycodeboy's avatar
crazycodeboy committed
55

Akshay Kadam's avatar
Akshay Kadam committed
56
```java
crazycodeboy's avatar
crazycodeboy committed
57
58
59
60
...
dependencies {
    ...
    compile project(':react-native-splash-screen')
crazycodeoby's avatar
update    
crazycodeoby committed
61
}
crazycodeboy's avatar
crazycodeboy committed
62
63
```

Jacob Lauritzen's avatar
Jacob Lauritzen committed
64
3. Update the MainApplication.java file to use `react-native-splash-screen` via the following changes:   
crazycodeboy's avatar
crazycodeboy committed
65
66

```java
Kyle Roach's avatar
Kyle Roach committed
67
68
69
// react-native-splash-screen >= 0.3.1
import org.devio.rn.splashscreen.SplashScreenReactPackage;
// react-native-splash-screen < 0.3.1
rex's avatar
rex committed
70
import com.cboy.rn.splashscreen.SplashScreenReactPackage;
Kyle Roach's avatar
Kyle Roach committed
71

crazycodeboy's avatar
crazycodeboy committed
72
73
74
75
76
77
78
79
80
81
82
83
public class MainApplication extends Application implements ReactApplication {

    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        @Override
        protected boolean getUseDeveloperSupport() {
            return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),
crazycodeboy's avatar
crazycodeboy committed
84
            new SplashScreenReactPackage()  //here
crazycodeboy's avatar
crazycodeboy committed
85
86
87
88
89
90
91
92
93
94
95
            );
        }
    };

    @Override
    public ReactNativeHost getReactNativeHost() {
        return mReactNativeHost;
    }
}
```

crazycodeboy's avatar
crazycodeboy committed
96
97
98
**iOS:**

1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
crazycodeoby's avatar
update    
crazycodeoby committed
99
2. Go to `node_modules``react-native-splash-screen` and add `SplashScreen.xcodeproj`
crazycodeboy's avatar
crazycodeboy committed
100
3. In XCode, in the project navigator, select your project. Add `libSplashScreen.a` to your project's `Build Phases``Link Binary With Libraries`
crazycodeboy's avatar
crazycodeboy committed
101
4. To fix `'SplashScreen.h' file not found`, you have to select your project → Build Settings → Search Paths → Header Search Paths to add:
102

crazycodeboy's avatar
crazycodeboy committed
103
   `$(SRCROOT)/../node_modules/react-native-splash-screen/ios`
crazycodeoby's avatar
crazycodeoby committed
104

crazycodeboy's avatar
crazycodeboy committed
105

crazycodeboy's avatar
crazycodeboy committed
106

crazycodeoby's avatar
update    
crazycodeoby committed
107
### Third step(Plugin Configuration):
crazycodeboy's avatar
crazycodeboy committed
108

crazycodeboy's avatar
crazycodeboy committed
109
110
**Android:**

Jacob Lauritzen's avatar
Jacob Lauritzen committed
111
Update the `MainActivity.java` to use `react-native-splash-screen` via the following changes:
crazycodeboy's avatar
crazycodeboy committed
112
113

```java
114
import android.os.Bundle; // here
115
import com.facebook.react.ReactActivity;
Kyle Roach's avatar
Kyle Roach committed
116
117
118
// react-native-splash-screen >= 0.3.1
import org.devio.rn.splashscreen.SplashScreen; // here
// react-native-splash-screen < 0.3.1
Jacob Lauritzen's avatar
Jacob Lauritzen committed
119
import com.cboy.rn.splashscreen.SplashScreen; // here
120

crazycodeboy's avatar
crazycodeboy committed
121
122
123
public class MainActivity extends ReactActivity {
   @Override
    protected void onCreate(Bundle savedInstanceState) {
crazycodeboy's avatar
crazycodeboy committed
124
        SplashScreen.show(this);  // here
crazycodeboy's avatar
crazycodeboy committed
125
126
        super.onCreate(savedInstanceState);
    }
crazycodeboy's avatar
crazycodeboy committed
127
    // ...other code
crazycodeboy's avatar
crazycodeboy committed
128
129
130
}
```

crazycodeboy's avatar
crazycodeboy committed
131
132
**iOS:**

Jacob Lauritzen's avatar
Jacob Lauritzen committed
133
Update `AppDelegate.m` with the following additions:
crazycodeboy's avatar
crazycodeboy committed
134

crazycodeoby's avatar
update    
crazycodeoby committed
135

crazycodeboy's avatar
crazycodeboy committed
136
137
138
139
140
141
142
143
144
145
146
```obj-c

#import "AppDelegate.h"
#import "RCTRootView.h"
#import "SplashScreen.h"  // here

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // ...other code
crazycodeoby's avatar
update    
crazycodeoby committed
147

crazycodeboy's avatar
crazycodeboy committed
148
149
150
151
152
153
154
    [SplashScreen show];  // here
    return YES;
}

@end

```
crazycodeboy's avatar
crazycodeboy committed
155
156
157
158
159
160
161

## Getting started  

Import `react-native-splash-screen` in your JS file.

`import SplashScreen from 'react-native-splash-screen'`    

crazycodeboy's avatar
crazycodeboy committed
162
### Android:
crazycodeboy's avatar
crazycodeboy committed
163

Jacob Lauritzen's avatar
Jacob Lauritzen committed
164
Create a file called `launch_screen.xml` in `app/src/main/res/layout` (create the `layout`-folder if it doesn't exist). The contents of the file should be the following:
crazycodeboy's avatar
crazycodeboy committed
165

Akshay Kadam's avatar
Akshay Kadam committed
166
```xml
crazycodeboy's avatar
crazycodeboy committed
167
168
169
170
171
172
173
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/launch_screen">
</LinearLayout>
```
crazycodeboy's avatar
crazycodeboy committed
174

Jacob Lauritzen's avatar
Jacob Lauritzen committed
175
176
177
178
179
180
181
182
183
Customize your launch screen by creating a `launch_screen.png`-file and placing it in an appropriate `drawable`-folder. Android automatically scales drawable, so you do not necessarily need to provide images for all phone densities.
You can create splash screens in the following folders:
* `drawable-ldpi`
* `drawable-mdpi`
* `drawable-hdpi`
* `drawable-xhdpi`
* `drawable-xxhdpi`
* `drawable-xxxhdpi`

crazycodeboy's avatar
crazycodeboy committed
184
185
**Optional steps:**

Jacob Lauritzen's avatar
Jacob Lauritzen committed
186
If you want the splash screen to be transparent, follow these steps.
crazycodeboy's avatar
crazycodeboy committed
187

Jacob Lauritzen's avatar
Jacob Lauritzen committed
188
Open `android/app/src/main/res/values/styles.xml` and add `<item name="android:windowIsTranslucent">true</item>` to the file. It should look like this:
crazycodeboy's avatar
crazycodeboy committed
189
190
191
192
193
194
195

```xml
<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <!--设置透明背景-->
Jacob Lauritzen's avatar
Jacob Lauritzen committed
196
        <item name="android:windowIsTranslucent">true</item>
crazycodeboy's avatar
crazycodeboy committed
197
198
199
200
    </style>
</resources>
```

Jacob Lauritzen's avatar
Jacob Lauritzen committed
201
**To learn more see [examples](https://github.com/crazycodeboy/react-native-splash-screen/tree/master/examples)**
crazycodeoby's avatar
update    
crazycodeoby committed
202
203


204
205
If you want to customize the color of the status bar when the splash screen is displayed:

206
Create `android/app/src/main/res/values/colors.xml` and add
207
208
209
```xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
210
    <color name="status_bar_color"><!-- Colour of your status bar here --></color>
211
212
213
</resources>
```

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
Create a style definition for this in `android/app/src/main/res/values/colors.xml`:
```xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="SplashScreenTheme" parent="SplashScreen_SplashTheme">
        <item name="colorPrimaryDark">@color/status_bar_color</item>
    </style>
</resources>
```

Change your `show` method to include your custom style:
```java
SplashScreen.show(this, false, R.style.SplashScreenTheme);
```

crazycodeboy's avatar
crazycodeboy committed
229
### iOS    
crazycodeoby's avatar
update    
crazycodeoby committed
230

Jacob Lauritzen's avatar
Jacob Lauritzen committed
231
232
233
Customize your splash screen via LaunchImage or LaunchScreen.xib,

**Learn more to see [examples](https://github.com/crazycodeboy/react-native-splash-screen/tree/master/examples)**
crazycodeboy's avatar
crazycodeboy committed
234

Jacob Lauritzen's avatar
Jacob Lauritzen committed
235
## Usage
crazycodeboy's avatar
crazycodeboy committed
236

Jacob Lauritzen's avatar
Jacob Lauritzen committed
237
Use like so:
crazycodeboy's avatar
crazycodeboy committed
238

Akshay Kadam's avatar
Akshay Kadam committed
239
```javascript
crazycodeboy's avatar
crazycodeboy committed
240
241
242
243
244
import SplashScreen from 'react-native-splash-screen'

export default class WelcomePage extends Component {

    componentDidMount() {
Jacob Lauritzen's avatar
Jacob Lauritzen committed
245
246
    	// do stuff while splash screen is shown
        // After having done stuff (such as async tasks) hide the splash screen
crazycodeboy's avatar
crazycodeboy committed
247
248
249
250
251
252
253
254
255
        SplashScreen.hide();
    }
}
```

## API


Method            | Type     | Optional | Description
crazycodeoby's avatar
update    
crazycodeoby committed
256
----------------- | -------- | -------- | -----------
DevIo.org's avatar
DevIo.org committed
257
show()   | function | false | Open splash screen (Native Method )
crazycodeboy's avatar
crazycodeboy committed
258
259
260
261
hide() |  function  | false  |  Close splash screen     

## Contribution

Jacob Lauritzen's avatar
Jacob Lauritzen committed
262
Issues are welcome. Please add a screenshot of you bug and a code snippet. Quickest way to solve issue is to reproduce it in one of the examples.
crazycodeboy's avatar
crazycodeboy committed
263

Jacob Lauritzen's avatar
Jacob Lauritzen committed
264
Pull requests are welcome. If you want to change the API or do something big it is best to create an issue and discuss it first.
crazycodeboy's avatar
crazycodeboy committed
265
266
267
268

---

**MIT Licensed**