React Native React-Navigation:在 Tab Navigator Tab 中添加徽章

React Native's React-Navigation: Adding a badge in TabNavigator's Tab

在带有 React-Navigation 的 React-Native 中,我有一个像这样的 Tabnavigator:

const testScreenNavigator = TabNavigator({
    Tab1: { screen: Tab1Screen },
    Tab2: { screen: Tab2Screen },
    Tab3: { screen: Tab3Screen },
});
testScreenNavigator.navigationOptions = {
    title: 'MY TITLE',
    header: {
        titleStyle:{
        },
        style:{
// how to set the options?
        },
    }  
}

现在我想在 Tab1 旁边添加一个徽章:例如

标签 1 (2) |标签 2 | Tab3

在 Android 中,可以通过以下方式完成:

 static navigationOptions = {

     tabBar: {
          label: () => {
                ... 
                return (
                   <Text style={{ backgroundColor: '...', borderRadius: 10}}>
                       {badgeNumber}
                   </Text>
                ...

在 iOS 中,它在底部显示 TabMenu,这没问题,因为它是 iOS 的本机行为。但是在 iOS 中没有显示徽章的圆圈,而是一个矩形背景。

为什么 and/or 如何在 iOS 中制作徽章?

此致

RN里面其实有一个badge-package: https://github.com/react-native-component/react-native-smart-badge.

import React, {Component } from 'react';
import {Animated ,Text,View,AppRegistry,Button, StyleSheet,Image } from 'react-native';

//  Badge 
export default class App extends Component {
    state = {
        badgeScale : new Animated.Value(0),
        textValue :0,
    }

    animatedBadge(){
        this.state.badgeScale.setValue(0);
        const newTextValue = ++this.state.textValue
        this.setState({textValue: newTextValue})
        Animated.timing(this.state.badgeScale , {
                toValue : 1,
                duration : 500
        }).start()
    }

    render(){
        const msize = 40;
        return(
            <View style={styles.container}>
                <View style={{ width :100, height :100, borderRadius :50, margin:10,}}>
                 <View
                            style={{ width :100, height :100, backgroundColor:'green', borderRadius :50,}}
                        /> 
                    {/* <Image
                          source={require('./circle.png')} // style={imageStyle}
                            style={{ width :100, height :100, borderRadius :50,}}
                        /> */}
                        <Animated.View style={{
                                position: 'absolute', width:msize, height:msize,
                                borderRadius:msize/2, backgroundColor:'black',
                                justifyContent:'center', alignContent:'center', 
                                borderColor:'green',borderWidth:1, 
                                // left:0, top:0,
                                left:70, top:0,     
                                // using this change bedge position 
                                    transform:[
                                        {
                                                scale:this.state.badgeScale
                                        }
                                    ]
                            }}>
                            <Text style={{backgroundColor :'transparent' ,                                  
                                            textAlign:'center',
                                        color:'red'}}>
                                    {this.state.textValue}
                            </Text>
                        </Animated.View>
                        <Button style={{ flex:1 , marginTop:50,justifyContent:'center',
                                                        alignContent:'center', }} 
                                                        title='Add' 
                                         onPress={ () =>this.animatedBadge() }>
                        </Button>                                                   
                </View>
            </View>
            );
        }
}
const styles = StyleSheet.create({
        container:{
            flex:1,
            justifyContent:'center',
            alignItems :'center',
            // backgroundColor:'#F5FCFF'
        },
        imageStyle :{
            width:200,
            height:200,
        },
        viewTextStyle:{
            position : 'absolute',
            justifyContent:'center',
            alignItems:'center',
        },
        textStyle:{
            fontSize:23,
            fontWeight:'bold',
            color:'white'
        }
})