我这个modal弹窗弹出来后面的状态栏没有被遮挡

来源:7-16 Modal 自定义弹窗

佚__名

2023-04-14

我这个modal弹窗此时弹出来后面的状态栏没有被遮挡
图片描述

视频里的状态栏也被挡住了,
图片描述

我记得我这边刚开始状态栏也是被modal挡住了,不知道什么时候就失效了,是我的代码有问题还是这又是rn debug模式下的bug?

完整代码如下:

import React, {useState} from 'react';
import {
  SectionList,
  StyleSheet,
  View,
  Modal,
  Text,
  Button,
  TouchableOpacity,
  Image,
} from 'react-native';
import {SectionData} from '../constants/Data';
import icon_close_modal from '../assets/images/icon_close_modal.png';

export default () => {
  const [visible, setVisible] = useState(false);

  const showModal = () => {
    setVisible(true);
  };

  const hideModal = () => {
    setVisible(false);
  };

  const renderItem = ({item}) => {
    return <Text style={styles.txt}>{item}</Text>;
  };

  const ListHeader = (
    <View style={styles.header}>
      <Text style={styles.extraTxt}>列表头部</Text>
      <TouchableOpacity style={styles.closeBtn} onPress={() => hideModal()}>
        <Image style={styles.closeImg} source={icon_close_modal} />
      </TouchableOpacity>
    </View>
  );

  const ListFooter = (
    <View style={[styles.header, styles.footer]}>
      <Text style={styles.extraTxt}>列表尾部</Text>
    </View>
  );

  const renderSectionHeader = ({section}) => {
    return <Text style={styles.sectionHeaderTxt}>{section.type}</Text>;
  };

  return (
    <View style={styles.root}>
      <Button title={'按钮'} onPress={() => showModal()} />

      <Modal visible={visible} onRequestClose={() => hideModal()}>
        <View style={styles.content}>
          <SectionList
            style={styles.sectionList}
            sections={SectionData}
            renderItem={renderItem}
            keyExtractor={(item, index) => `${item}-${index}`}
            contentContainerStyle={styles.containerStyle}
            showsVerticalScrollIndicator={false}
            ListHeaderComponent={ListHeader}
            ListFooterComponent={ListFooter}
            renderSectionHeader={renderSectionHeader}
            ItemSeparatorComponent={() => <View style={styles.separator} />}
            stickySectionHeadersEnabled={true}
          />
        </View>
      </Modal>
    </View>
  );
};

const styles = StyleSheet.create({
  root: {
    width: '100%',
    height: '100%',
  },
  content: {
    width: '100%',
    height: '100%',
    backgroundColor: '#FF000030',
  },
  sectionList: {
    width: '100%',
    height: '100%',
  },
  txt: {
    width: '100%',
    height: 56,
    fontSize: 20,
    color: '#333333',
    textAlignVertical: 'center',
  },
  containerStyle: {
    paddingHorizontal: 16,
    paddingTop: 20,
    backgroundColor: '#F5F5F5',
  },
  header: {
    width: '100%',
    height: 48,
    backgroundColor: '#00FF0030',
    justifyContent: 'center',
    alignItems: 'center',
  },
  footer: {
    backgroundColor: '#FF000030',
  },
  extraTxt: {
    fontSize: 20,
    color: '#666666',
    textAlignVertical: 'center',
    textAlign: 'center',
  },
  sectionHeaderTxt: {
    width: '100%',
    height: 36,
    backgroundColor: '#DDDDDD',
    textAlign: 'center',
    textAlignVertical: 'center',
    fontSize: 20,
    color: '#333333',
    fontWeight: 'bold',
  },
  separator: {
    width: '100%',
    height: 1,
    backgroundColor: '#D0D0D0',
  },
  closeImg: {
    width: 24,
    height: 24,
  },
  closeBtn: {
    width: 24,
    height: 24,
    position: 'absolute',
    right: 16,
  },
});

写回答

1回答

FE大公爵

2023-04-15

仔细看课程哦,有个属性控制的,statusbar开头的,你再去看下。

0
2
FE大公爵
回复
佚__名
确认一下状态栏颜色模式,如果也没问题,那暂时判断不出来,可以在QQ群里把代码发出来
2023-04-16
共2条回复

RN从0到1系统精讲与小红书APP实战

30+小案例+2个实战项目,快人一步提升个职业竞争力

295 学习 · 211 问题

查看课程