首页 > 解决方案 > 标题栏不可见

问题描述

我对 React Native 很陌生,我现在正在尝试一些东西。我还让第一个演示应用程序运行顺利。
现在我想测试教程中的示例,并在右上角添加一个带有窗口标题的标题栏和一个按钮。为此,我只是扩展了 MyNewProject。但是,如果我现在启动应用程序,顶部不会出现任何栏,只会显示窗口中间的文本。我究竟做错了什么?

这是程序代码:

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
  android:
    'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

type Props = {};
export default class App extends Component<Props> {
  static navigationOptions = {
    title: 'My new project',
    headerRight: (
      <Button
        onPress={() => Alert.alert('This is a button!')}
        title='Info'
      />
    ),
  };
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <Text style={styles.instructions}>To get started, edit App.js</Text>
        <Text style={styles.instructions}>{instructions}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

标签: react-nativeview

解决方案


尝试改变这个

static navigationOptions = {
    title: 'My new project',
    headerRight: (
      <Button
        onPress={() => Alert.alert('This is a button!')}
        title='Info'
      />
    ),
  };

对此:

static navigationOptions = props => {  return {
    title: 'My new project',
    headerRight: (
      <Button
        onPress={() => Alert.alert('This is a button!')}
        title='Info'
      />
    ),   
   } 
 };

推荐阅读