0

我正在使用 UI 的本机基础创建本机反应应用程序(http://nativebase.io/docs/v2.0.0/components#footerTab)。我正在使用 footerTabs 组件,我的代码如下

render() {
    return (

 <Container>
    <Header backgroundColor="#ECEFF1">
      <Button transparent>
            <Icon name='ios-menu' style={{color: 'black'}}/>
      </Button>
      <Title style={{color:'black'}}>Header</Title>
  </Header>

    <Content>
        <Profile/>
    </Content>

    <Footer backgroundColor="#212121">
      <FooterTab>
        <Button backgroundColor="#000" >
           <Icon name="ios-person" size={30} color="#900"/>
           <Text>Profile</Text>
        </Button>

         <Button>
              <Icon name="ios-search"/>
              <Text>Search</Text>
          </Button>

        <Button>
            <Icon name="ios-camera"/>
            <Text>Camera</Text>
        </Button>

        <Button>
             <Icon name="ios-apps"/>
             <Text>Apps</Text>
        </Button>

        <Button>
            <Icon active name="ios-navigate"/>
            <Text>Navigate</Text>
        </Button>

    </FooterTab>
    </Footer>
  </Container>
);
}

我为配置文件、搜索、应用程序等不同功能创建了不同的 JS 文件,并按如下方式导入它们。

import Profile from './Profile';

如何在页脚按钮上实现 onPress 功能,以根据选择的内容更改内容标签中的组件?

 <Content>
    <Profile/>
</Content>

例如:如果我按下搜索按钮,我希望将配置文件标签替换为其他按钮,并且类似地相同。

4

3 回答 3

2

在这里,来自本机基础的 FooterTab 不会像 iOS 中的 UITabBar 那样保留实际的选项卡功能,它只保留用于设计。您需要做的是,使用所有四个按钮在所有组件中保留页脚标签,并相应地保持活动状态。要通过选择任何按钮来更改视图,您需要使用导航器将当前视图替换为选定的视图,例如:

<Button onPress={()=> { this.props.navigator.replace({id:'component name'}) }}>

并且在您的导航器组件中,您应该根据路由有效负载中的 id 值在 renderScene 方法中定义所有必需的组件。如果您想要 TabBar 工作的实际功能,那么您可以使用这个第三方模块react-native-tab-navigator。谢谢!

于 2017-02-07T18:05:47.043 回答
1

与其替换内容,不如让每个 Button 导航到新页面?

假设您在“个人资料”选项卡上。你可以这样做:

import FooterWrapper from './FooterWrapper'

<Footer>
  <FooterWrapper tab='profile' navigator={this.props.navigator} />
</Footer>

然后在您的 FooterWrapper 中(我刚刚处理了两个选项卡的情况):

constructor(props) {
  super(props)
  this.state = {
    profileTab: this.props.tab === 'profile',
    searchTab: this.props.tab === 'search',
  }
} 

navToProfilePage() {
  this.props.navigator.push({
    id: 'profile',
    tab: 'profile',
  })
}

navToSearchPage() {
  this.props.navigator.push({
    id: 'search',
    tab: 'search',
  })
}

render() {
  return (
    <FooterTab>
      <Button active={this.state.profileTab} onPress={() => this.navToProfilePage()}>
          Profile
          <Icon name='ios-person' size={30} color='#900' />
      </Button> 
      <Button active={this.state.searchTab} onPress={() => this.navToSearchPage()}>
          Search
          <Icon name='ios-search' />
      </Button>
    </FooterTab>
  )
}
于 2017-02-07T19:47:01.003 回答
0

好的,这就是我是如何得到它的,我使用内容标签中的 renderContent 方法根据单击按钮时的状态变化生成视图。

 <Content>
        {this._renderContent(this.state.selectedTab)}        
 </Content>

selectedTab 是一个状态变量,其状态是使用 onPress 方法中的 this.setState 设置的。renderContent 有一个 if 函数,用于检查选定的选项卡并返回适当的视图。我也尝试了导航方法,但这看起来更干净。

 _renderContent = (Tab: string,) => {
    if(this.state.selectedTab==="Profile"){
    return (
      <Profile/>
    );
    }
    else if(this.state.selectedTab==="Search"){

    }
}
于 2017-02-08T08:56:47.873 回答