【问题标题】:React navigation make transparent screen inside other StackNavigator反应导航在其他 StackNavigator 中制作透明屏幕
【发布时间】:2018-06-11 08:05:08
【问题描述】:

我在其他 StackNagigation 中遇到透明屏幕问题。 demo

单击 ScreenTwo 中的 Go to ScreenThree 按钮后,我想在 ScreenTwo 前面显示 ScreenThree 叠加层。

我已将cardStyle 设置为backgroundColor: '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'

    },
})

【问题讨论】:

  • 你能简要介绍一下你想要达到的目标吗
  • 你想让屏幕 3 覆盖屏幕 2 并且屏幕 3 是透明的吗?
  • @Revansiddh 是的。我希望屏幕 3 是透明的并覆盖在屏幕 2 的前面。

标签: react-native react-navigation


【解决方案1】:

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

  1. 我使用的是 react-navigator v3,cardStyle: { backgroundColor: 'transparent'} 没有帮助。所以我不得不选择transparentCard: true
  2. 从开发工具检查和修改屏幕的backgroundColor 将显示所有屏幕背景为transparent,但它们仍然是白色的。所以我发现在屏幕过渡期间,过渡结束时的每个屏幕都会添加一个白色背景的容器(仅在 iOS 和 Web 中发生)。

    所以,我必须将这个过渡容器的背景也设置为透明 -> Source code

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

另外需要注意的是您需要先将这 2 条规则应用于 非常顶级的导航器。然后你只需修改backgroundColor 为 无论您想要透明还是其他颜色的屏幕:D

【讨论】:

    【解决方案2】:

    将此粘贴​​到您的 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>
    
    

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

    【讨论】:

      【解决方案3】:

      这对我有用 - 取自 Github issue

      // create custom transitioner without the opacity animation, ie. for iOS
      function forVertical(props) {
        const { layout, position, scene } = props;
      
        const index = scene.index;
        const height = layout.initHeight;
      
        const translateX = 0;
        const translateY = position.interpolate({
          inputRange: ([index - 1, index, index + 1]: Array<number>),
          outputRange: ([height, 0, 0]: Array<number>)
        });
      
        return {
          transform: [{ translateX }, { translateY }]
        };
      }
      
      // set it as the transitionConfig param on your stack navigator
      const App = StackNavigator(
        {
          Modal: { screen: Modal }
        },
        {
          mode: "modal",
          headerMode: "none",
          transitionConfig: () => ({ screenInterpolator: forVertical })
        }
      );
      

      【讨论】:

        【解决方案4】:

        这应该会给你你想要的东西

        更新:transitionConfig 需要是一个函数,就我而言,我需要添加空的 screenInterpolator 函数。

        const Root = StackNavigator({
          Main: {screen: MainStack}
        },
        {
            mode: 'modal',
            headerMode: 'none',
            cardStyle: {
              shadowColor: 'transparent'
            },
            transitionConfig: ({ scene }) => ({
              transitionSpec: {
                duration: 0,
                timing: Animated.timing,
                easing: Easing.step0,
              },
            screenInterpolator: screenProps => {
              return {}
            }
          })
        })
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-26
        • 1970-01-01
        • 1970-01-01
        • 2020-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多