【问题标题】:How can I get TabBarIOS to pop to the top of the navigation stack using react-native?如何使用 react-native 让 TabBarIOS 弹出到导航堆栈的顶部?
【发布时间】:2016-11-22 21:55:54
【问题描述】:

我有以下 TabBarIOS.Item 设置:

          <TabBarIOS.Item
            selected={this.state.selectedTab === 'tab1'}
            title='Tab 1'
            icon={require('./Components/Icons/IconImages/Tab1Icon.png')}
            onPress={() => {
              this.setState({
                selectedTab: 'tab1'
              });

             }}>
            <MyNavigatorIOS client={this.state.client} initialStep={this.state.initialStep} />

          </TabBarIOS.Item>

我正在尝试根据this example in the react native docs 使用onPress 事件来触发this.props.navigator.popToTop();。但是,不同之处在于我希望 TabBarIOS onPress 事件触发 popToTop() 而不是子 MyNavigatorIOS 组件。我怎样才能做到这一点?

【问题讨论】:

    标签: react-native tabbar tabbarios


    【解决方案1】:

    我遇到了同样的问题,你可以做的是为你的视图和其中的导航器添加一个引用,如下所示:

              this.refs.timeline.refs.timelineNavigator.popToTop()
    

    所以 TabBar 代码如下所示:

        <TabBarIOS.Item
          systemIcon="history"
          title="Timeline"
          badge={this.state.timelineBadge}
          selected={this.state.selectedTab === 'timeline'}
          onPress={() => {
            if (this.state.selectedTab === 'timeline') {
              this.refs.timeline.refs.timelineNavigator.popToTop()
            } else {
              this.setState({ selectedTab: 'timeline' })
            }
          }}>
          <Timeline
            ref="timeline"
          /> 
        </TabBarIOS.Item>
    

    【讨论】:

    • 这看起来可行!我最终通过使用EventEmitter 并在适当的子屏幕中处理事件来解决它。不过这看起来更好 =)
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2020-02-19
    • 2018-12-09
    • 2020-12-07
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多