3

在带有 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) | Tab2 | Tab3

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

 static navigationOptions = {

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

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

为什么会这样和/或如何在 iOS 中完成徽章?

问候

4

2 回答 2

0

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

于 2017-05-10T10:50:24.507 回答
0

在此处输入图像描述

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'
        }
})
于 2020-09-05T17:37:07.933 回答