我正在使用带有React-Navigation模块的React-Native(Expo)开发一个应用程序。我正在努力获得“ createBottomTabBar”样式所需的内容,并致力于自定义标签栏组件。
在哪里可以找到/找到类似的示例?我的代码正确吗?
我正在使用这个Border bottom on Bottom Navigation in React Native和这个视频:https://www.youtube.com/watch?v=w24FE9PZpzk
但是我不知道该怎么做。
Router / index.js
import { createSwitchNavigator,createStackNavigator, createBottomTabNavigator, createDrawerNavigator, createAppContainer } from 'react-navigation';
import { Home, SignUpScreen, LoginScreen, ForgotPasswordScreen, Setting, AboutScreen, ScanFlight, FlightDetails, ChatList, ChatMessages, FriendList, exploreProfile, myProfile, FlightAdd, Notifications,Achievements } from '../Screens';
// Tab bar Custom Component
import appBottomTabs from '../NavigationLayout/bottomTabBar'
// Bottom Tab navigation
const MainTabNavigator = createBottomTabNavigator({
Home,
ChatList,
ScanFlight
}, {
tabBarOptions: {
activeTintColor: "#6200EE",
inactiveTintColor: "#858585",
style: {
paddingVertical: 10,
backgroundColor: "#fff",
border: '#ffffff'
},
labelStyle: {
fontSize: 14,
lineHeight: 16,
fontFamily: "Rubik_Regular"
},
tabBarPosition: "bottom",
tabBarComponent: appBottomTabs,
animationEnabled: true,
swipeEnabled: true,
unmountInactiveRoutes: true
}
});
// Drawer Navigation
const appDrawerNavigator = createDrawerNavigator({
Home: {screen: MainTabNavigator},
Login: LoginScreen,
SignUp: SignUpScreen,
ForgotPassword: ForgotPasswordScreen,
Settings: Setting,
About: AboutScreen,
FlightDetails: FlightDetails,
FlightAdd: FlightAdd,
}, {
unmountInactiveRoutes: true
});
const AppLoginNavigator = createSwitchNavigator({
LoginScreen,
ForgotPasswordScreen,
appDrawerNavigator: {
screen: appDrawerNavigator
},
appStackNavigator: {
screen: appStackNavigator
}
});
const AppContainer = createAppContainer(AppLoginNavigator);
class App extends Component {
render() {
return (
<AppContainer />
);
}
}
export default App;
NavigationLayout / bottomTabBar.js
import React, {Component} from 'react'
import {View, TouchableWithoutFeedback, Text, StyleSheet} from 'react-native'
class appBottomTabs extends Component {
constructor(){
super()
this.state = {
routeNameSelected:'Home'
}
}
onPressTab(routeName){
this.props.jumpTo(routeName)
this.setState({
routeNameSelected:routeName
})
}
render() {
const {navigation} = this.props;
const {routes} = navigation.state;
return (
<View style={styles.tabbar}>
{routes && routes.map((route, index) => {
return (
<TouchableWithoutFeedback
key={route.key}
style={styles.tab}
onPress={() => this.onPressTab(route.routeName)}
>
</TouchableWithoutFeedback>
);
})}
</View>
)
}
}
export default appBottomTabs;
这是我要实现的布局:https://postimg.cc/Mfc5HxPs
活动链接具有更大的字体大小和底部边框。
答案 0 :(得分:2)
因此,您需要在defaultNavigationOptions
中使用createBottomTabNavigator
。
defaultNavigationOptions包含一个功能/反应组件,您可以在其中返回组件。
例如
const renderNav = (routeName, name, tintColor, focused) => (
<View style={{flex: 1, alignItems: 'center', borderBottomColor: focused ? tintColor: '', borderBottomWidth: focused ? 4 : 0}}>
<Icon name={name} color={tintColor} size={12} style={{paddingBottom: 4, paddingTop: 10}} />
<Text style={{paddingBottom: 8}}>{routeName}</Text>
</View>
)
const customTabs = ({ navigation }) => ({
tabBarIcon: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
if (routeName === 'Profile') {
return renderNav(routeName, 'user', tintColor, focused);
} else if (routeName === 'Home') {
return renderNav(routeName, 'home', tintColor, focused);
} else if (routeName === 'History') {
return renderNav(routeName, 'history', tintColor, focused);
} else if (routeName === 'Cart') {
return renderNav(routeName, 'shopping-cart', tintColor, focused);
}
}
});
const DashboardTabNav = createBottomTabNavigator({
Profile: {
screen: ProfileScreen
},
Home: Dashboard,
History: SettingsScreen,
Cart: CartScreen
},
{
defaultNavigationOptions: customTabs,
animationEnabled: true,
swipeEnabled: true,
tabBarPosition: 'bottom',
initialRouteName: 'Cart',
tabBarOptions: {
activeTintColor: '#6C1D7C',
inactiveTintColor: 'rgba(0,0,0,0.6)',
showLabel: false,
style:{
shadowColor: 'rgba(58,55,55,0.1)',
shadowOffset: { width: 0, height: 0 },
shadowOpacity: 1,
shadowRadius: 15,
elevation: 3,
borderTopColor: 'transparent',
backgroundColor:'#fff',
borderTopLeftRadius: 20,
borderTopRightRadius: 20,
height: 50
},
activeTabStyle: {
backgroundColor: 'white',
borderBottomWidth: 4,
borderColor: '#6C1D7C'
}
},
});