react navigation三种导航,其中TabNavigation和StackNavigation用的比较多。今天要说的就是关于TabNavigation的样式问题,可能很多人都遇到过为什么明明设置了style和tabStyle还是会有问题。
我想可能会有很多人这样写:
const tabbaroption = {
activeTintColor: Color.f3474b,
inactiveTintColor: Color.C7b7b7b,
showIcon: true,
style: {
backgroundColor:'white'
},
indicatorStyle: {
opacity: 0
},
iconStyle:{
paddingTop:0,
padding:0,
marginTop:0,
width:SCALE(45),
height:SCALE(45),
},
labelStyle:{
marginTop:0,
padding:0,
},
tabStyle: {
height:Platform.OS==='ios'?SCALE(90):SCALE(100),
alignItems:'center',
justifyContent:'center',
}
};
//我发起的请假选项
const LeaveListIndex = TabNavigator(
{
LeaveUnCheckList: { screen: LeaveUnCheckList },
LeaveFinished:{screen: LeaveFinished}
},
{
lazy: true,
tabBarPosition: 'bottom',
animationEnabled: true,
tabBarOptions: tabbaroption,
headerLeft:null,
});
然后满心欢喜的以为图片和文字会居中,然而残酷的现实告诉我们并不是这样。得到的结果大概是这样的。
tabstyle.png
这个时候你可能会设置各种背景颜色,设置margin,设置padding距离,然后发现终于成功了,在其他机型运行时又tm出现适配问题。哈哈哈哈。
所以今天我们就自己定义一个tab替换掉默认的。
修改代码:
const LeaveListIndex = TabNavigator(
{
LeaveUnCheckList: { screen: LeaveUnCheckList },
LeaveFinished:{screen: LeaveFinished}
},
{
lazy: true,
tabBarComponent:props => <Tab {...props}/>,
tabBarPosition: 'bottom',
animationEnabled: true,
tabBarOptions: tabbaroption,
headerLeft:null,
});
自定义的Tab:
export default class Tab extends Component {
renderItem = (route, index) => {
const {
navigation,
jumpToIndex,
} = this.props;
const focused = index === navigation.state.index;
const color = focused ? this.props.activeTintColor : this.props.inactiveTintColor;
let TabScene = {
focused:focused,
route:route,
tintColor:color
};
return (
<TouchableOpacity
key={route.key}
style={styles.tabItem}
onPress={() => jumpToIndex(index)}
>
<View
style={styles.tabItem}>
{this.props.renderIcon(TabScene)}
<Text style={{ ...styles.tabText,marginTop:SCALE(10),color }}>{this.props.getLabel(TabScene)}</Text>
</View>
</TouchableOpacity>
);
};
render(){
const {navigation,} = this.props;
const {routes,} = navigation.state;
return (
<View style={styles.tab}>
{routes && routes.map((route,index) => this.renderItem(route, index))}
</View>
);
}
}
const styles = {
tab:{
borderTopWidth:StyleSheet.hairlineWidth,
borderTopColor:Color.dddddd,
width:WIDTH,
height: Platform.OS==='ios'?SCALE(90):SCALE(100),
backgroundColor:'white',
flexDirection:'row',
justifyContent:'space-around',
alignItems:'center'
},
tabItem:{
width:SCALE(100),
alignItems:'center',
justifyContent:'center'
},
tabText:{
marginTop:SCALE(13),
fontSize:FONT(10),
color:Color.C7b7b7b
},
tabTextChoose:{
color:Color.f3474b
},
tabImage:{
width:SCALE(42),
height:SCALE(42),
},
}
然后一切都好了,世界安静了。
tabright.png
网友评论