Change app background color in React Native
Asked Answered
N

8

46

I'm trying to change the color of the background in my React Native app, from grey to white. I'm using react-navigation to make a TabNavigator after I render it. I tried to put this TabNavigator in a view and set backgroundColor but all screen became white. How can I solve this?

index.js

    render() {
        return (
            <View style={{ backgroundColor: '#FFFFFF'}}>
                <Tabs />
            </View>
        )
      }

Tabs

    const Tabs = TabNavigator(
      {
        Home: {
          screen: HomeStack,
          navigationOptions: {
            title: 'Acasa',
          },
        },
        ...
        Account: {
          screen: AccountScreen,
          navigationOptions: {
            title: 'Contul meu',
          },
        },
      },
      {
        tabBarComponent: props => <FooterNavigation {...props} />,
        tabBarPosition: 'bottom',
        initialRouteName: 'Home',
      },
    );

Home Screen

    render() {
        const {
          data, refreshing, loading, error,
        } = this.state;
    
        return (
          <ScrollView>
            <Container>
              {error && <Text>Error</Text>}
              {loading && <ActivityIndicator animating size="large" />}
    
              <List>
                <FlatList
                  data={data}
                  renderItem={({ item }) => (
                    <ListItem onPress={() => this.props.navigation.navigate('Item', item)}>
                      <Item data={item} />
                    </ListItem>
                  )}
                  // ID from database as a key
                  keyExtractor={item => item.title}
                  ItemSeparatorComponent={this.renderSeparator}
                  ListFooterComponent={this.renderFooter}
                  ListHeaderComponent={this.renderHeader}
                  refreshing={refreshing}
                  onRefresh={this.handleRefresh}
                  onEndReached={this.handleLoadMore}
                  onEndReachedThreshold={0}
                />
              </List>
            </Container>
          </ScrollView>
        );
      }
Naumann answered 31/5, 2018 at 8:14 Comment(7)
<View style={{backgroundColor : "white"}}> <Tabs/> </View>Ancestral
I have tried, and all screen became white imgur.com/a/faBqkLtNaumann
Can you show the home screen code?Cranmer
where you have added background color : white ?Cantor
I tried in index.js, where is <Tabs />, i did something like <View style={{ backgroundColor: "#FFFFFF"}}><Tabs /></View>, but it made my screen all white, not only backgroundNaumann
what's your expected result?Ancestral
Well, by default, on ios device, for example, is a gray background, I want to change it to whiteNaumann
N
43

I've solved my problem, it was caused by StackNavigator. To solve it , just add some extra options

const HomeStack = StackNavigator(
      {
        Home: {
          screen: HomeScreen,
        },
        Item: {
          screen: ItemScreen,
          navigationOptions: ({ navigation }) => ({
            title: `${navigation.state.params.title}`,
          }),
        },
      },
      **
      {
        headerMode: 'screen',
        cardStyle: { backgroundColor: '#FFFFFF' },
      },
      **
    );
Naumann answered 7/6, 2018 at 7:50 Comment(0)
C
32

For React Navigation 6,

<Stack.Navigator  screenOptions={{
     contentStyle:{
       backgroundColor:'#FFFFFF'
     }
  }}  initialRouteName="Home">
Conversationalist answered 17/9, 2021 at 10:34 Comment(1)
Thanks, the above options didn't work for meKrell
M
26

For React Navigation 5 and above

    <Stack.Navigator
        initialRouteName='dashboard'
        screenOptions={{
            headerStyle: { elevation: 0 },
            cardStyle: { backgroundColor: '#fff' }
        }}
    >
        <Stack.Screen name="Home" component={HomeStack} />
    </Stack.Navigator>

For React Navigation 4 and earlier

    const HomeStack = StackNavigator({
        Home: {
            screen: HomeScreen,
        },
    },
    {
        headerMode: 'screen',
        cardStyle: { backgroundColor: '#fff' },
    },
    );
Manipulator answered 16/9, 2020 at 14:35 Comment(0)
B
11

Edit your View tag like this,

  <View style={{flex: 1,backgroundColor: '#6ED4C8'}}></View>
Beowulf answered 2/3, 2019 at 9:49 Comment(0)
J
7

For React Navigation 6 -->

Stack Navigator:

<Stack.Navigator 
    screenOptions={{ contentStyle: {backgroundColor: 'white'} }}
>

Tab Navigator:

<Tab.Navigator
    sceneContainerStyle={{backgroundColor: 'white'}}
    ...
>
Jenevajeni answered 15/8, 2023 at 9:41 Comment(0)
S
5

The correct prop to be set is sceneContainerStyle:

  <BottomTab.Navigator
    ...
    sceneContainerStyle={{ backgroundColor: 'white' }}
  >
      ...
   </BottomTab.Navigator>
Shanghai answered 19/5, 2022 at 22:27 Comment(0)
S
4

The following will no longer work due to deprecation.

 const HomeStack = StackNavigator(
  {
    Home: {
      screen: HomeScreen,
    },
    Item: {
      screen: ItemScreen,
      navigationOptions: ({ navigation }) => ({
        title: `${navigation.state.params.title}`,
      }),
    },
  },
  **
  {
    headerMode: 'screen',
    cardStyle: { backgroundColor: '#FFFFFF' },
  },
  **
);

You now have to use defaultNavigationOptions (see below).

 const HomeStack = StackNavigator(
  {
    Home: {
      screen: HomeScreen,
    },
    Item: {
      screen: ItemScreen,
      navigationOptions: ({ navigation }) => ({
        title: `${navigation.state.params.title}`,
      }),
    },
  },
  **
  {
    headerMode: 'screen',
    defaultNavigationOptions: {
      cardStyle: { backgroundColor: '#FFFFFF' },
    },
  },
  **
);
Subsidy answered 10/1, 2020 at 14:9 Comment(0)
R
2

Set in view that's where you want to set background colour

view: {
  backgroundColor: '#FFFFFF' 
}
Regularize answered 29/3, 2021 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.