反应导航模态高度

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

如何将高度设置为React Navigation模态视图,一旦它出现,它将仅从底部向上覆盖屏幕的大约一半,并且下面的视图仍然可见?

更新:我正在尝试创建类似于App Store购买模式的ux流程,其中某种StackNavigator嵌套在填充屏幕下半部分的模式中。

App Store modal

react-native modal-dialog react-navigation
3个回答
7
投票

在stacknavigator中,您可以设置以下选项:

  mode: 'modal',
    headerMode: 'none',
    cardStyle:{
      backgroundColor:"transparent",
      opacity:0.99
  }

在您的模态屏幕中:

class ModalScreen extends React.Component {

  render() {
    return (
      <View style={{ flex: 1 ,flexDirection: 'column', justifyContent: 'flex-end'}}>
          <View style={{ height: "50%" ,width: '100%', backgroundColor:"#fff", justifyContent:"center"}}>
            <Text>Testing a modal with transparent background</Text>
          </View>
      </View>
    );
  }
}

你也可以参考我创建的这个世博小吃https://snack.expo.io/@yannerio/modal来展示它是如何工作的,或者你可以使用React Native Modal


3
投票

以下是如何在react-navigation v3.x中实现此目的的示例:

demo

App容器

const TestRootStack = createStackNavigator(
  {
    TestRoot: TestRootScreen,
    TestModal: {
      screen: TestModalScreen,
      navigationOptions: {
        /**
         * Distance from top to register swipe to dismiss modal gesture. Default (135)
         * https://reactnavigation.org/docs/en/stack-navigator.html#gestureresponsedistance
         */
        gestureResponseDistance: { vertical: 1000 }, // default is 135 },
      },
    },
  },
  {
    headerMode: 'none',
    mode: 'modal',
    transparentCard: true,
  },
);

const AppContainer = createAppContainer(TestRootStack);

根屏幕

class TestRootScreen extends React.Component {
  render() {
    return (
      <SafeAreaView style={styles.container}>
        <Button title="Show Modal" onPress={() => this.props.navigation.navigate('TestModal')} />
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'blue',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

模态屏幕

class TestModalScreen extends React.Component {
  render() {
    return (
      <SafeAreaView style={styles.container}>
        <View style={styles.innerContainer} />
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'transparent',
    justifyContent: 'flex-end',
  },
  innerContainer: {
    position: 'absolute',
    bottom: 0,
    left: 0,
    right: 0,
    top: 100,
    backgroundColor: 'red',
  },
});


0
投票

对于react-navigation v3.x,你可以使用prop transparentCard:true,你可以在这里看到更多细节:https://stackoverflow.com/a/55598127/6673414

© www.soinside.com 2019 - 2024. All rights reserved.