反应导航使透明屏幕在其他StackNavigator中

问题描述 投票:2回答:3

我在其他StackNagigation中的透明屏幕有问题。 demo

我想在ScreenTwo中单击ScreenThree按钮后在ScreenTwo前面显示Go to 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'

    },
})
react-native react-navigation
3个回答
2
投票

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

  1. 我正在使用react-navigator v3和cardStyle: { backgroundColor: 'transparent'}没有帮助。所以我不得不选择transparentCard: true
  2. 从devtools检查和修改屏幕的backgroundColor将显示所有屏幕背景为transparent但它们仍然是白色的。所以我发现在屏幕转换期间,转换端的每个屏幕都会添加一个带有白色背景的容器(仅在iOS和Web中发生)。 因此,我必须将此过渡容器的背景设置为透明 - > Source code

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

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


0
投票

这对我有用 - 取自这个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 })
  }
);

0
投票

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

更新: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 {}
    }
  })
})
© www.soinside.com 2019 - 2024. All rights reserved.