我正在使用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 回答
这里来自本机基础的FooterTab并不像iOS中的UITabBar那样持久保存实际标签功能,它只能坚持设计 . 您需要做的是,使用所有四个按钮在所有组件中保留页脚标记并相应地保持活动状态 . 要通过选择任何按钮来更改视图,您需要使用导航器替换当前视图,例如:
在导航器组件中,您应该根据路由有效负载中的id值在renderScene方法中定义所有必需的组件 . 如果您希望实际功能作为TabBar工作,那么您可以使用此第三方模块react-native-tab-navigator . 谢谢!
而不是替换内容,为什么不让每个Button导航到新页面?
假设您在“ Profiles ”标签上 . 你可以这样做:
然后在你的FooterWrapper中(我刚刚处理了两个标签的情况):
好的,这就是我如何得到它我在内容标签中使用renderContent方法来生成视图,具体取决于单击按钮时的状态更改 .
selectedTab是一个状态变量,其状态是使用onPress方法中的this.setState设置的 . renderContent有一个if函数,用于检查选定的选项卡并返回相应的视图 . 我也试过导航方法,但这看起来更干净 .