react-navigation导航头顶部的绝对位置

Tur*_*ets 6 javascript absolute reactjs react-native react-navigation

我正在制作加载屏幕,并将其设置为整个屏幕的绝对位置。但是,使用react-navigation它似乎并不覆盖标题。有没有一种方法可以将我的组件放置在导航库的标题组件之上?

使用时,react-navigation您可以为该屏幕配置标题。通常,当您导航到屏幕时,您在该屏幕中呈现的所有代码都会自动放置在nav标头下方。但是,我希望组件占据整个屏幕并覆盖标题。我想保留标题,但要用不透明性覆盖它。这可能吗?

const navigationOptions = {
  title: "Some Title",
};

    const Navigator = StackNavigator(
  {
    First: { screen: ScreenOne },
    Second: { screen: ScreenTwo },
    ...otherScreens,
  },
  {
    transitionConfig,
    navigationOptions, //this sets my header that I want to cover
  }
);
Run Code Online (Sandbox Code Playgroud)

这是我的loader.js

const backgroundStyle = {
  opacity: 0.5,
  flex: 1,
  position: 'absolute',
  top: 0,
  left: 0,
  right: 0,
  bottom: 0,
  zIndex: 1,
};

const Loading = () =>
  <View style={backgroundStyle}> 
      <PlatformSpinner size="large" />
  </View>;
Run Code Online (Sandbox Code Playgroud)

在ScreenOne.js中

class ScreenOne extends Component { 
  render(){
   if(loading) return <Loading/>;
   return (
     //other code when not loading that is placed under my header automatically
   )
  }
}
Run Code Online (Sandbox Code Playgroud)

Rav*_*Raj 8

从您所理解的问题出发,您想在所有其他组件(包括具有不透明性的导航标题)之上渲染微调器组件。

一种实现方法是渲染一个Modal包装您的的组件spinner。模态组件将全屏显示,您可以提供道具transparent = true。并自定义模态的父视图,background colour with opacity如图所示。现在,在各处显示/隐藏此Modal组件以处理加载。

使用零食的演示:https : //snack.expo.io/SyZxWnZPZ

下面的示例代码

import React, { Component } from 'react';
import { View, StyleSheet,Modal,ActivityIndicator,Button } from 'react-native';
import { Constants } from 'expo';

export default class App extends Component {
  state = {
    isShowModal: false,
  }
  render() {
    return (
      <View style={styles.container}>
        <Button title='show modal' onPress={() => this.setState({isShowModal: true})} />
        {this.state.isShowModal && this.showModal()}
      </View>
    );
  }

  showModal() {
    setTimeout(() => this.setState({isShowModal: false}), 5000); // just to mimic loading
    return(
      <Modal
        animationType='fade'
        transparent={true}
        visible={true}>

        <View style={{flex:1,backgroundColor:'rgba(0,0,0,.2)'}}>
          <ActivityIndicator size='large' color='red' style={{flex:1}} />
        </View>
      </Modal>
    )
  }
}



const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
  },
});
Run Code Online (Sandbox Code Playgroud)