最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - absolute position on top of react-navigation nav header - Stack Overflow

programmeradmin9浏览0评论

I'm making a loading screen and I've set it to absolute position the entire screen. However when using react-navigation it does not seem to cover the header. Is there a way to place my ponent on top of the navigation library's header ponent?

When you use react-navigation you can configure a header for that screen. Usually when you navigate to a screen, any code you render within that screen is automatically placed underneath the nav header. However, I want my ponent to take the whole screen and cover the header. I want to header to remain, but I want to cover it with an opacity. Is this possible?

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

Here's my 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>;

In ScreenOne.js

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

I'm making a loading screen and I've set it to absolute position the entire screen. However when using react-navigation it does not seem to cover the header. Is there a way to place my ponent on top of the navigation library's header ponent?

When you use react-navigation you can configure a header for that screen. Usually when you navigate to a screen, any code you render within that screen is automatically placed underneath the nav header. However, I want my ponent to take the whole screen and cover the header. I want to header to remain, but I want to cover it with an opacity. Is this possible?

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

Here's my 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>;

In ScreenOne.js

class ScreenOne extends Component { 
  render(){
   if(loading) return <Loading/>;
   return (
     //other code when not loading that is placed under my header automatically
   )
  }
}
Share Improve this question edited Aug 3, 2017 at 19:04 Turnipdabeets asked Aug 2, 2017 at 18:13 TurnipdabeetsTurnipdabeets 6,0059 gold badges45 silver badges65 bronze badges 5
  • Can you elaborate with an example of what do you have and what do you want to achieve? – lilezek Commented Aug 2, 2017 at 18:16
  • sure, added more – Turnipdabeets Commented Aug 2, 2017 at 18:24
  • Why your width and height is in viewport units and also in percentage units? – lilezek Commented Aug 2, 2017 at 19:30
  • someone else suggested viewport so i added it, but it didn't do anything either way – Turnipdabeets Commented Aug 2, 2017 at 19:33
  • To use viewport (height, for instance) you write: 100vh, not 100vh%. – lilezek Commented Aug 2, 2017 at 19:34
Add a ment  | 

1 Answer 1

Reset to default 17

From your question what I understand is, you want to render a spinner ponent above all other ponents including the navigation header with opacity.

One way to do this is render a Modal ponent which wraps your spinner. Modal ponent takes the full screen and you can give props transparent = true. And customise the Parent View of Modal to have background colour with opacity as shown. Now show/hide this Modal ponent everywhere to handle loading.

Demo using snack : https://snack.expo.io/SyZxWnZPZ

Sample code below

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',
  },
});
发布评论

评论列表(0)

  1. 暂无评论