Parcourir la source

Add docs about using custom navigation buttons (#1669)

Eli Perkins il y a 7 ans
Parent
révision
d5cfcbec67
1 fichiers modifiés avec 57 ajouts et 0 suppressions
  1. 57
    0
      docs/adding-buttons-to-the-navigator.md

+ 57
- 0
docs/adding-buttons-to-the-navigator.md Voir le fichier

@@ -55,6 +55,7 @@ class FirstTabScreen extends Component {
55 55
   rightButtons: [{ // buttons for the right side of the nav bar (optional)
56 56
     title: 'Edit', // if you want a textual button
57 57
     icon: require('../../img/navicon_edit.png'), // if you want an image button
58
+    component: 'example.CustomButton', // if you want a custom button (iOS only)
58 59
     id: 'compose', // id of the button which will pass to your press event handler. See the section bellow for Android specific button ids
59 60
     testID: 'e2e_is_awesome', // if you have e2e tests, use this to find your button
60 61
     disabled: true, // optional, used to disable the button (appears faded and doesn't interact)
@@ -62,6 +63,7 @@ class FirstTabScreen extends Component {
62 63
     buttonColor: 'blue', // Set color for the button (can also be used in setButtons function to set different button style programatically)
63 64
     buttonFontSize: 14, // Set font size for the button (can also be used in setButtons function to set different button style programatically)
64 65
     buttonFontWeight: '600', // Set font weight for the button (can also be used in setButtons function to set different button style programatically)
66
+    passProps: {}, // Object that will be passed as props to custom components (iOS only, optional)
65 67
   }],
66 68
   leftButtons: [] // buttons for the left side of the nav bar (optional)
67 69
 }
@@ -75,6 +77,61 @@ On Android, four button types are supported by default without the need to provi
75 77
 * accept
76 78
 * sideMenu
77 79
 
80
+#### Custom Navigation Buttons - iOS only
81
+
82
+react-native-navigation uses `UIBarButtonItem` to display all items in the navigation bar. Instead of using images or text for normal `UIBarButtonItem`s, you can supply a custom component to be displayed within a custom view of a `UIBarButtonItem`, using the `component` property when specifying a navigation button.
83
+
84
+Custom components must first be registered, just as screens are registered, using [`Navigation.registerComponent`](#/top-level-api?id=registercomponentscreenid-generator-store-undefined-provider-undefined).
85
+
86
+Additionally, ensure that the view is able to size itself, as custom navigation buttons will size depending on their content. Only `width` will be respected by the navigation bar; views can overflow outside of the navigation bar if they are too tall.
87
+
88
+```js
89
+const styles = StyleSheet.create({
90
+  button: {
91
+    overflow: 'hidden',
92
+    width: 34,
93
+    height: 34,
94
+    borderRadius: 34 / 2,
95
+    justifyContent: 'center',
96
+    alignItems: 'center',
97
+  },
98
+});
99
+
100
+// Our custom component we want as a button in the nav bar
101
+const CustomButton = ({ text }) =>
102
+  <TouchableOpacity
103
+    style={[styles.button, { backgroundColor: 'tomato' }]}
104
+    onPress={() => console.log('pressed me!')}
105
+  >
106
+    <View style={styles.button}>
107
+      <Text style={{ color: 'white' }}>
108
+        {text}
109
+      </Text>
110
+    </View>
111
+  </TouchableOpacity>;
112
+
113
+// Register the component
114
+Navigation.registerComponent('CustomButton', () => CustomButton);
115
+
116
+Navigation.startSingleScreenApp({
117
+  screen: {
118
+    screen: 'example.screen',
119
+    title: 'React Native Navigation',
120
+    navigatorButtons: {
121
+      leftButtons: [
122
+        {
123
+          id: 'custom-button',
124
+          component: 'CustomButton', // This line loads our component as a nav bar button item
125
+          passProps: {
126
+            text: 'Hi!',
127
+          },
128
+        },
129
+      ],
130
+    },
131
+  },
132
+});
133
+```
134
+
78 135
 #### Floating Action Button (FAB) - Android only
79 136
 Each screen can contain a single Fab which is displayed at the bottom right corner of the screen.
80 137