Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React navigation make transparent screen inside other StackNavigator

I have problem with transparent screen inside other StackNagigation. demo

I want to show ScreenThree overlay in the front of ScreenTwo after click Go to ScreenThree button in ScreenTwo.

I have set cardStyle with backgroundColor: 'transparent' but it still doesn't working.

I dont know what's wrong here? Have anyone please help me?

import { StackNavigator } from 'react-navigation'; // 2.2.5


import React from 'react'
import { Image, View, Text, Button } from 'react-native'
import { StyleSheet, Dimensions, TouchableOpacity } from 'react-native'

export default class App extends React.Component {
  render() {
    return (
      <View style={{flex: 1, backgroundColor: 'red'}}>
        <Root/>
      </View>
    )
  }
}

class HomeScreen extends React.Component {

  render() {

    return (
      <View style={{
        backgroundColor: 'blue', 
        flex: 1, 
        justifyContent: 'center', 
        alignItems: 'center',
        paddingTop: 20
      }}>

      <TouchableOpacity onPress={
        () => { 
        this.props.navigation.navigate('ScreenTwo')}
      } 
      style={{
        padding: 16, 
        backgroundColor: 'gray'
      }}>
        <Text>
          Go to ScreenTwo
        </Text>
      </TouchableOpacity>
      </View>
      )
  }
}

class ScreenTwo extends React.Component {

  render() {

    return (
      <View style={{
        flex: 1, 
        justifyContent: 'center', 
        alignItems: 'center'
      }}>
        <Text style={{color: 'white'}}>
          ScreenTwo
        </Text>
        <TouchableOpacity onPress={
        () => { 
        this.props.navigation.navigate('ScreenThree')}
      } 
      style={{
        padding: 16, 
        backgroundColor: 'gray',
        marginTop: 16
      }}>
        <Text>
          Go to ScreenThree
        </Text>
      </TouchableOpacity>

      </View>
      )
  }
}

class ScreenThree extends React.Component {

  render() {

    return (
      <View style={{
        backgroundColor: 'rgba(0,0,0,0.3)', 
        flex: 1, 
        justifyContent: 'center', 
        alignItems: 'center'
      }}>
        <Text style={{color: 'white'}}>
          ScreenThree
        </Text>

      </View>
      )
  }
}

const DetailStack = StackNavigator({
  ScreenThree: {screen: ScreenThree}
}, {
  mode: 'modal',
    headerMode: 'none',
    cardStyle: {
      backgroundColor: 'transparent',
      shadowColor: 'transparent'
    },
})

const MainStack = StackNavigator({
  HomeScreen: {screen: HomeScreen},
  ScreenTwo: {screen: ScreenTwo},
DetailStack: {screen: DetailStack}
},
{
  headerMode: 'none',
  cardStyle: {shadowColor: 'transparent'},
})

const Root = StackNavigator({
  Main: {screen: MainStack}
},
{
    mode: 'modal',
    headerMode: 'none',
    cardStyle: {
      shadowColor: 'transparent'

    },
})
like image 872
Phan Sinh Avatar asked Jan 02 '23 07:01

Phan Sinh


2 Answers

I was having this issue too and took me a couple of days to find out why. There were 2 different factors:

  1. I was using the react-navigator v3 and cardStyle: { backgroundColor: 'transparent'} didn't help. So I had to go with the transparentCard: true
  2. Inspecting and modifying backgroundColor of the screen from the devtools would show all screen background to be transparent but they were still white. So what I found was that during screen transition, a container with white background is added to every screen on transition end (only happens in iOS and web).

    So, I had to set the background of this transition container to be transparent too -> Source code

transitionConfig: () => ({
  containerStyle: {
    backgroundColor: 'transparent'
  }
})

Another thing to note is you need to apply these 2 rules first to the very topmost navigator. Then you just modify the backgroundColor for whatever screen you want to be transparent or of other color :D

like image 145
Bijay Timilsina Avatar answered Jan 05 '23 16:01

Bijay Timilsina


Paste this inside your ModalStackScreen. You can use this like a boiler plate.


<Stack.Navigator
  screenOptions={{
    headerShown: false,
    cardStyle: { backgroundColor: 'transparent' },
    cardOverlayEnabled: true,
    cardStyleInterpolator: ({ current: { progress } }) => ({
      cardStyle: {
        opacity: progress.interpolate({
          inputRange: [0, 0.5, 0.9, 1],
          outputRange: [0, 0.25, 0.7, 1],
        }),
      },
      overlayStyle: {
        opacity: progress.interpolate({
          inputRange: [0, 1],
          outputRange: [0, 0.5],
          extrapolate: 'clamp',
        }),
      },
    }),
  }}
  mode="modal"
>
  <Stack.Screen name="Home" component={HomeStack} />
  <Stack.Screen name="Modal" component={ModalScreen} />
</Stack.Navigator>

For more clarity/details read https://reactnavigation.org/docs/stack-navigator/ It's explained so well

like image 44
Shravya Ramesh Avatar answered Jan 05 '23 16:01

Shravya Ramesh