使用react-navigation动态改变当前状态栏title

前言

正在使用react native构建自己的第二款APP,遇到了之前没有遇到的一些问题,就是点击下面的tabbar的时候,上面的状态栏问题无法动态改变。查阅了一些资料也没有头绪,最后去官网看了一下文档,解决后为有同样问题的大家分享一下步骤。


步骤

1. 重构navigationOptions使得可以接收参数

static navigationOptions = ({ navigation }) => {
        const { params } = navigation.state;

        return {
            title: params ? params.title : '新闻',            #最关键的一句,剩下的属性自己可以自定义添加
                headerStyle: {
                    backgroundColor: '#fff',
                },
                headerTintColor: '#000',
                headerTitleStyle: {
                    flex: 1,
                    textAlign: 'center',
                    fontWeight: '20',
                },
        }
    };

2. 自定义方法,改变title的值

_gotoPage(title) {
        this.props.navigation.setParams({title: title});
    }

使用onPress调用方法

onPress={this._gotoPage.bind(this, '消息')}

或者方便起见,也可以不定义方法,直接在onPress中调用:

onPress={() => this.props.navigation.setParams({title: '消息'})}

此时,点击下面labbar上面的title已经可以自动改变了。



我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3pgogufbqg2so

猜你喜欢

转载自blog.csdn.net/qq_33876553/article/details/80215445