React Navigation modal height

后端 未结 4 1158
执念已碎
执念已碎 2020-12-31 07:33

How do I set the height a React Navigation modal view so once it has appeared it will only cover about half of the screen from the bottom up, and the view below remains visi

相关标签:
4条回答
  • 2020-12-31 08:10

    if you want to use react native Modal you can make parent view transparent and add a view at the bottom

    <Modal
          animationType="slide"
          transparent={true}
          visible={props.visible}
        >
         <View
              style={{
                 backgroundColor:'transparent',
                 flex:1,
                 justifyContent:'flex-end'
                     }}>
              <View
                   style={{
                       backgroundColor:'green',
                       height:'20%'
                     }}>
                   <Text>Hello World!</Text>
                   <TouchableHighlight
                        onPress={props.closeModal}>
                         <Text>Hide Modal</Text>
                   </TouchableHighlight>
              </View>
        </View>
     </Modal>
    
    0 讨论(0)
  • 2020-12-31 08:13

    For react-navigation v3.x you can use the prop transparentCard: true, you can see more details here: https://stackoverflow.com/a/55598127/6673414

    0 讨论(0)
  • 2020-12-31 08:25

    Here's an example of how to achieve this in react-navigation v3.x:

    App Container

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

    Root Screen

    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',
      },
    });
    

    Modal Screen

    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 讨论(0)
  • 2020-12-31 08:37

    In your stacknavigator you can set these options:

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

    And in your modal screen:

    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>
        );
      }
    }
    

    Also you can refer to this expo snack https://snack.expo.io/@yannerio/modal that I've created to show how it works, or you could use React Native Modal

    0 讨论(0)
提交回复
热议问题