StackWithCustomHeaderBackImage.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @flow
  3. */
  4. import type { NavigationScreenProp } from 'react-navigation';
  5. import * as React from 'react';
  6. import { Image, Button, StatusBar, StyleSheet } from 'react-native';
  7. import { createStackNavigator, SafeAreaView } from 'react-navigation';
  8. import SampleText from './SampleText';
  9. type MyNavScreenProps = {
  10. navigation: NavigationScreenProp<*>,
  11. banner: React.Node,
  12. };
  13. class MyCustomHeaderBackImage extends React.Component<any, any> {
  14. render() {
  15. const source = require('./assets/back.png');
  16. return (
  17. <Image
  18. source={source}
  19. style={[styles.myCustomHeaderBackImage, this.props.style]}
  20. />
  21. );
  22. }
  23. }
  24. class MyNavScreen extends React.Component<MyNavScreenProps> {
  25. render() {
  26. const { navigation, banner } = this.props;
  27. return (
  28. <SafeAreaView>
  29. <SampleText>{banner}</SampleText>
  30. <Button
  31. onPress={() => navigation.navigate('Photos', { name: 'Jane' })}
  32. title="Navigate to a photos screen"
  33. />
  34. <Button onPress={() => navigation.goBack(null)} title="Go back" />
  35. <StatusBar barStyle="default" />
  36. </SafeAreaView>
  37. );
  38. }
  39. }
  40. type MyHomeScreenProps = {
  41. navigation: NavigationScreenProp<*>,
  42. };
  43. class MyHomeScreen extends React.Component<MyHomeScreenProps> {
  44. static navigationOptions = {
  45. title: 'Welcome',
  46. headerBackTitle: null,
  47. };
  48. render() {
  49. const { navigation } = this.props;
  50. return <MyNavScreen banner="Home Screen" navigation={navigation} />;
  51. }
  52. }
  53. type MyPhotosScreenProps = {
  54. navigation: NavigationScreenProp<*>,
  55. };
  56. class MyPhotosScreen extends React.Component<MyPhotosScreenProps> {
  57. static navigationOptions = ({ navigation }) => ({
  58. title: `${navigation.state.params.name}'s photos`,
  59. headerBackTitle: null,
  60. });
  61. render() {
  62. const { navigation } = this.props;
  63. return (
  64. <SafeAreaView>
  65. <SampleText>{`${navigation.state.params.name}'s Photos`}</SampleText>
  66. <Button
  67. onPress={() => navigation.navigate('Profile', { name: 'Jane' })}
  68. title="Navigate to a profile screen"
  69. />
  70. <Button onPress={() => navigation.goBack(null)} title="Go back" />
  71. <StatusBar barStyle="default" />
  72. </SafeAreaView>
  73. );
  74. }
  75. }
  76. type MyProfileScreenProps = {
  77. navigation: NavigationScreenProp<*>,
  78. };
  79. class MyProfileScreen extends React.Component<MyProfileScreenProps> {
  80. static navigationOptions = ({ navigation }) => ({
  81. title: 'Profile',
  82. headerBackImage: (
  83. <MyCustomHeaderBackImage style={styles.myCustomHeaderBackImageAlt} />
  84. ),
  85. });
  86. render() {
  87. const { navigation } = this.props;
  88. return (
  89. <SafeAreaView>
  90. <SampleText>{`${navigation.state.params.name}'s Profile`}</SampleText>
  91. <Button onPress={() => navigation.goBack(null)} title="Go back" />
  92. <StatusBar barStyle="default" />
  93. </SafeAreaView>
  94. );
  95. }
  96. }
  97. const StackWithCustomHeaderBackImage = createStackNavigator(
  98. {
  99. Home: {
  100. screen: MyHomeScreen,
  101. },
  102. Photos: {
  103. path: 'photos/:name',
  104. screen: MyPhotosScreen,
  105. },
  106. Profile: {
  107. path: 'profile/:name',
  108. screen: MyProfileScreen,
  109. },
  110. },
  111. {
  112. navigationOptions: {
  113. headerBackImage: MyCustomHeaderBackImage,
  114. },
  115. }
  116. );
  117. export default StackWithCustomHeaderBackImage;
  118. const styles = StyleSheet.create({
  119. myCustomHeaderBackImage: {
  120. height: 14.5,
  121. width: 24,
  122. marginLeft: 9,
  123. marginRight: 12,
  124. marginVertical: 12,
  125. resizeMode: 'contain',
  126. },
  127. myCustomHeaderBackImageAlt: {
  128. tintColor: '#f00',
  129. },
  130. });