我在React Native应用程序中使用React Navigation,我想将标题的backgroundColor更改为渐变,我发现有一个节点模块:react-native-linear-gradient以实现反应原生的渐变
我有这样的Root StackNavigator:
const Router = StackNavigator({
Login: {
screen: Login,
navigationOptions: ({navigation}) => ({
headerTitle: <Text>SomeTitle</Text>
headerLeft: <SearchAndAgent />,
headerRight: <TouchableOpacity
onPress={() => { null }
</TouchableOpacity>,
headerStyle: { backgroundColor: '#005D97' },
}),
},
});
我可以将Text或View设置为渐变:
<LinearGradient colors={['#3076A7', '#19398A']}><Text style={styles.title}>{title}</Text></LinearGradient>,
如何在navigationOptions中包含标题背景以供使用 LinearGradient模块?
我知道我可以创建一个自定义标题组件并使用它但是当我这样做时,React Navigation的所有原生导航动画都不像两个路线之间的标题动画那样工作,所以它没有帮助我。
感谢您的帮助!
答案 0 :(得分:18)
仅为了您的信息,现在使用headerBackground
道具更容易。
您可以使用渐变标题:
navigationOptions: {
headerBackground: (
<LinearGradient
colors={['#a13388', '#10356c']}
style={{ flex: 1 }}
start={{x: 0, y: 0}}
end={{x: 1, y: 0}}
/>
),
headerTitleStyle: { color: '#fff' },
}
即使使用适用于IosX的SafeArea
,此解决方案也能正常运行答案 1 :(得分:5)
Mark P的解决方案是正确的,但现在你需要定义headerStyle并在那里进行绝对定位:
navigationOptions: {
header: props => <GradientHeader {...props} />,
headerStyle: {
backgroundColor: 'transparent',
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},
},
和GradientHeader:
const GradientHeader = props => (
<View style={{ backgroundColor: '#eee' }}>
<LinearGradient
colors={['red', 'blue']}
style={[StyleSheet.absoluteFill, { height: Header.HEIGHT }]}
>
<Header {...props} />
</LinearGradient>
</View>
)
答案 2 :(得分:4)
与此问题类似:React Navigation; use image in header?
对于线性渐变,您只需执行&gt;
//imports
import { Image, StyleSheet, View } from 'react-native';
import { Header } from 'react-navigation' ;
import LinearGradient from 'react-native-linear-gradient';
//header
创建包含在Linear Gradient中的Header组件。
通过制作标题backgroundColor: 'transparent'
,您将显示包裹它的线性渐变。
const GradientHeader = props => (
<View style={{ backgroundColor: '#eee' }}>
<LinearGradient
colors={['#00a8c3', '#00373f']}
style={[StyleSheet.absoluteFill, styles.linearGradient]}
/>
<Header {...props} style={{ backgroundColor: 'transparent' }}/>
</View>
);
返回屏幕,标题为GradientHeader
组件。
const SimpleStack = StackNavigator({
Home: {
screen: MyHomeScreen,
},
}, {
navigationOptions: {
headerTitleStyle: { color: '#fff' },
header: (props) => <GradientHeader {...props} />,
}
});
使用上面的代码应该看起来像这样。
答案 3 :(得分:0)
您可以使用博览会中的LinearGradient
组件。这是一个有用的组件,您无法安装其他库,例如react-native-linear-gradient。 https://docs.expo.io/versions/latest/sdk/linear-gradient/。顺便说一句,您可以更改后退按钮。很简单。
您可以使用类似的navigationOptions在内部屏幕上实现
static navigationOptions = ({ navigation }: any) => {
const onGoBack = () => navigation.goBack();
return {
header: (props: any) => <GradientHeader {...props} />,
headerStyle: { height: 68, backgroundColor: "transparent", color: colors.white },
headerTitle: "Sign Up",
headerTitleStyle: { color: colors.white, fontSize: 18 },
headerLeft: (
<TouchableOpacity style={{ width: 32, height: 32, paddingLeft: 8 }} onPress={onGoBack}>
<Image source={images.back} resizeMode="center" style={{ width: 32, height: 32 }} />
</TouchableOpacity>
),
};
};