反应导航使其他StackNavigator内的屏幕透明

Pha*_*inh 3 react-native react-navigation

我在其他StackNagigation内的透明屏幕有问题。 演示

我想在ScreenTwo中单击按钮后ScreenThree在前面显示覆盖。ScreenTwoGo to ScreenThree

我已经设置好了cardStylebackgroundColor: 'transparent'但仍然无法正常工作。

我不知道这是怎么了?有没有人请帮助我?

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'

    },
})
Run Code Online (Sandbox Code Playgroud)

Bij*_*ina 5

我也遇到了这个问题,花了几天的时间找出原因。有两个不同的因素:

  1. 我当时正在使用react-navigator v3,cardStyle: { backgroundColor: 'transparent'}但并没有帮助。所以我不得不去transparentCard: true
  2. backgroundColor从devtools 检查和修改屏幕将显示所有屏幕背景为transparent白色,但仍为白色。因此,我发现在屏幕过渡期间,将白色背景的容器添加到过渡端的每个屏幕(仅在iOS和网络中发生)。

    因此,我也必须将这个过渡容器的背景设置为透明-> 源代码

transitionConfig: () => ({
  containerStyle: {
    backgroundColor: 'transparent'
  }
})
Run Code Online (Sandbox Code Playgroud)

要注意的另一件事是,您需要首先将这两个规则应用于最顶层的导航器。然后,您只需backgroundColor为要透明或其他颜色的屏幕修改:D


小智 5

将其粘贴到您的 ModalStackScreen 中。您可以像样板一样使用它。


<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>

Run Code Online (Sandbox Code Playgroud)

为了更清楚/详细信息,请阅读https://reactnavigation.org/docs/stack-navigator/它解释得很好