【问题标题】:absolute position on top of react-navigation nav headerreact-navigation 导航标题顶部的绝对位置
【发布时间】:2018-01-10 02:41:20
【问题描述】:

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

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

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

这是我的 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>;

在 ScreenOne.js 中

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

【问题讨论】:

  • 您能否举例说明您拥有什么以及您想要实现什么?
  • 当然,添加更多
  • 为什么你的宽度和高度是视口单位和百分比单位?
  • 其他人建议视口,所以我添加了它,但它没有做任何事情
  • 要使用视口(例如高度),请编写:100vh,而不是 100vh%

标签: javascript reactjs react-native absolute react-navigation


【解决方案1】:

根据您的问题,我的理解是,您希望在所有其他组件之上渲染一个微调器组件,包括具有不透明度的导航标题。

一种方法是渲染一个 Modal 组件,它包装了您的 spinner。模态组件占据全屏,你可以给道具transparent = true。并自定义 Modal 的父视图,使其具有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',
  },
});

【讨论】:

  • 谢谢,我忘记了即使在导航标题下方呈现其他内容时,Modal 也会覆盖所有内容。
  • 好答案。这就是我从文件中遗漏的内容。谢谢。
  • 完美。谢谢
猜你喜欢
  • 1970-01-01
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多