【问题标题】:React native - Hide status bar only on specific screensReact native - 仅在特定屏幕上隐藏状态栏
【发布时间】:2018-09-01 07:03:16
【问题描述】:

我正在使用标签导航来上传如下图片

const Photos = TabNavigator({
    CAMERA: {
            screen: TakeCamera,
        navigationOptions: {
          tabBarIcon: ({focused}) => (
            <View style={{flexDirection: 'row'}}>
              <Text style={{textAlign: 'center', color: focused? '#C7A985' : '#020202'}}>CAMERA</Text>
              <Icon name="chevron-down" size={15} color= {focused? '#C7A985' : '#ffffff'}/>
            </View>
          )
        },
        },
      ALBUMS: {
            screen: Albums,
        navigationOptions: {
          tabBarIcon: ({focused}) => (
            <View style={{flexDirection: 'row'}}>
              <Text style={{textAlign: 'center', color: focused? '#C7A985' : '#020202'}}>ALBUMS</Text>
              <Icon name="chevron-down" size={15} color= {focused? '#C7A985' : '#ffffff'}/>
            </View>
          )
        },
        },
    {
      tabBarOptions: {
        upperCaseLabel: false,
        showIcon: true,
        showLabel: false,
        style: {
          backgroundColor: '#F7F1ED',
          borderTopWidth: 1
        }
      },
        //initialRouteName: 'Feed',
      tabBarComponent: TabBarBottom,
      tabBarPosition: 'bottom',
      animationEnabled: false,
      swipeEnabled: false,
    });

export default class UploadPost extends Component {
  static navigationOptions = ({ navigation }) => ({
    header: null,
    tabBarVisible: false
  });

  render() {
    return (
      <View style={{flex: 1}}>
        <StatusBar hidden={true}/>
        <Photos screenProps={{navigation: this.props.navigation}}/>
      </View>
    );
  }
}

这里&lt;Statusbar hidden={true}&gt; 按预期隐藏了“CAMERA”、“ALBUMS”屏幕中的状态栏。但它也会隐藏其他屏幕中的状态栏。

  1. 当我打开应用程序时,我可以看到状态栏
  2. 在我打开 CAMERA 或 ALBUMS 屏幕后,状态栏对于所有其他屏幕将永久隐藏。

我的问题是,如何仅在 CAMERA、ALBUMS 屏幕上隐藏状态栏?

【问题讨论】:

    标签: react-native react-native-navigation


    【解决方案1】:

    您可以使用文档中提到的Screen Tracking Middleware 来获取currentScreen 的活动routeName

    function getActiveRouteName(navigationState) {
      if (!navigationState) {
        return null;
      }
      const route = navigationState.routes[navigationState.index];
      // dive into nested navigators
      if (route.routes) {
        return getActiveRouteName(route);
      }
      return route.routeName;
    }
    

    如果 routeName 与您不想在其中显示 StatusBar 的所需屏幕匹配,则将其设置为 true 否则 false

    【讨论】:

    • 您好,感谢您的回答。我需要在哪里添加上述功能?在 app.js 中还是在 index.js 中?
    • 您可以将其添加到定义导航器的同一屏幕并传递currentRoute,然后可以对其进行更新以检查它是否是所需的路线,请查看link了解更多详细信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    相关资源
    最近更新 更多