首页 文章

使用Native Base在Native React中实现页脚选项卡

提问于
浏览
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文件,例如Profiles,Search,Apps等,并按如下方式导入它们 .

import Profile from './Profile';

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

<Content>
    <Profile/>
</Content>

例如:如果我按下搜索按钮,我想要替换配置文件标签,并且对于其他按钮也是如此 .

3 回答

  • 0

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

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

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

  • 0

    而不是替换内容,为什么不让每个Button导航到新页面?

    假设您在“ Profiles ”标签上 . 你可以这样做:

    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>
      )
    }
    
  • 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"){
    
        }
    }
    

相关问题