react-navigation 导航顶部的绝对位置 header
absolute position on top of react-navigation nav header
我正在制作加载屏幕并将其设置为在整个屏幕上绝对定位。但是,当使用 react-navigation
时,它似乎没有覆盖 header。有没有办法将我的组件放在导航库的 header 组件之上?
当您使用 react-navigation
时,您可以为该屏幕配置 header。通常,当您导航到一个屏幕时,您在该屏幕中呈现的任何代码都会自动放置在导航下方 header。但是,我希望我的组件占据整个屏幕并覆盖 header。我想 header 保留,但我想用不透明度覆盖它。这可能吗?
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
)
}
}
根据你的问题,我的理解是,你想在所有其他组件之上渲染一个微调器组件,包括不透明的导航 header。
一种方法是渲染一个 Modal
组件来包装你的 spinner
。 Modal 组件占满屏幕,你可以给道具transparent = true
。并自定义 Modal 的 Parent 视图,使其具有 background colour with opacity
,如图所示。现在 show/hide 这个 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',
},
});
我正在制作加载屏幕并将其设置为在整个屏幕上绝对定位。但是,当使用 react-navigation
时,它似乎没有覆盖 header。有没有办法将我的组件放在导航库的 header 组件之上?
当您使用 react-navigation
时,您可以为该屏幕配置 header。通常,当您导航到一个屏幕时,您在该屏幕中呈现的任何代码都会自动放置在导航下方 header。但是,我希望我的组件占据整个屏幕并覆盖 header。我想 header 保留,但我想用不透明度覆盖它。这可能吗?
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
)
}
}
根据你的问题,我的理解是,你想在所有其他组件之上渲染一个微调器组件,包括不透明的导航 header。
一种方法是渲染一个 Modal
组件来包装你的 spinner
。 Modal 组件占满屏幕,你可以给道具transparent = true
。并自定义 Modal 的 Parent 视图,使其具有 background colour with opacity
,如图所示。现在 show/hide 这个 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',
},
});