在React Native绝对位置子视图中忽略安全区域

ruarlubt  于 2023-04-07  发布在  React
关注(0)|答案(1)|浏览(186)

我正在尝试创建覆盖在屏幕上。
但问题是儿童视图即弹出屏幕也有顶部和底部的安全区域添加。
我不想只在一个屏幕上使用它:https://www.npmjs.com/package/react-native-safe-area-context
代码如下:

const SomeScreen: FC = () => {
      const [isSuccess, setIsSuccess] = useState<boolean>(true);
    
      return (
        <SafeAreaView style={styles.container}>
          <View style={styles.container}>
             <Text style={styles.titleText}> Some Text </Text>
    
            { isSuccess ? (
              <View style={styles.popUpContainer}>
               <View style={styles.popUpBody}>
                 <Text style={styles.titleText}> {titleText} </Text>
                 <Text style={styles.subtext}> {subtext} </Text>
                 <Text style={styles.subtext}> {buttonText} </Text>
                 <Button title={buttonText} onPress={setIsSuccess(false);} />
               </View>
             </View>
            ) : null}
         </View>
       </SafeAreaView>
      );
    };

StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'orange',
  },
  popUpContainer: {
      height: '100%',
      width: '100%',
      position: 'absolute',
      flex: 1,
      backgroundColor: 'rgba(52, 52, 52, 0.6)',
      justifyContent: 'center',
    },
 popUpBody: {
      backgroundColor: '#fff',
      height: '50%',
      width: '80%',
      justifyContent: 'center',
    },
});

我尝试了可能的解决方案,它适用于某些设备,但它不是理想的解决方案:

popUpContainer: {
          height: '120%',
          width: '100%',
          position: 'absolute',
          top: - 50,
          flex: 1,
          backgroundColor: 'rgba(52, 52, 52, 0.6)',
          justifyContent: 'center',
        },
bvuwiixz

bvuwiixz1#

有两种方法可以做到这一点。
1.您可以使用Modal组件而不是绝对View
1.用View包裹SafeAreaView,如下所示:

const SomeScreen: FC = () => {
      const [isSuccess, setIsSuccess] = useState<boolean>(true);
    
      return (
       <View style={{flex: 1}}>
          <SafeAreaView style={styles.container}>
            <View style={styles.container}>
               <Text style={styles.titleText}> Some Text </Text>
            </View>
          </SafeAreaView>
          { isSuccess ? (
            <View style={styles.popUpContainer}>
             <View style={styles.popUpBody}>
               <Text style={styles.titleText}> {titleText} </Text>
               <Text style={styles.subtext}> {subtext} </Text>
               <Text style={styles.subtext}> {buttonText} </Text>
               <Button title={buttonText} onPress={setIsSuccess(false);} />
             </View>
           </View>
          ) : null}
       </View>
    );
  };

上更多的东西按钮onPress绑定是错误的。

<Button title={buttonText} onPress={setIsSuccess(false);} />

这一定是

<Button title={buttonText} onPress={() => setIsSuccess(false)} />

相关问题