ReactNative 自定义标题栏 ReactNative 自定义导航栏

2024-06-03 05:58

本文主要是介绍ReactNative 自定义标题栏 ReactNative 自定义导航栏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先上代码,配合代码讲解一下使用方法,
github地址:https://github.com/lizhuoyuan/react-native-navigationBar
我这有用到一个屏幕适配工具类,自己写的,使用方法和代码 请点这里

这里写图片描述
这里写图片描述

/*** Created by 李卓原 on 2018/7/6.* email: zhuoyuan93@gmail.com**/import React from 'react';
import {Text,StyleSheet,View,Image,ImageBackground,TouchableOpacity,Platform,StatusBar,ViewPropTypes
} from 'react-native';//ScreenUtil 为屏幕尺寸适配的工具类
import * as ScreenUtil from "../utils/ScreenUtil";
import PropTypes from 'prop-types';const NAV_BAR_HEIGHT_ANDROID = 50;
const NAV_BAR_HEIGHT_IOS = 44;const StatusBarShape = {barStyle: PropTypes.oneOf(['light-content', 'default', 'dark-content']),hidden: PropTypes.bool,backgroundColor: PropTypes.string,
};class NavigationBar extends React.Component {static propTypes = {statusBar: PropTypes.shape(StatusBarShape),showBackgroundIMG: PropTypes.bool,style: ViewPropTypes.style,title: PropTypes.string,titleView: PropTypes.element,titleLayoutStyle: ViewPropTypes.style,showLeft: PropTypes.bool,leftText: PropTypes.string,leftTextStyle: ViewPropTypes.style,showleftImg: PropTypes.bool,leftButton: PropTypes.element,showRight: PropTypes.bool,rightText: PropTypes.string,rightTextStyle: ViewPropTypes.style,rightButton: PropTypes.element,};static defaultProps = {statusBar: {barStyle: 'default',hidden: false,},showLeft: true,showleftImg: true,  //是否显示返回箭头leftText: '',   //返回键位置的文字showRight: false,rightText: '更多',showBackgroundIMG: true  //是否有背景图片};constructor(props) {super(props);}render() {let leftButton = this._renderLeft();let rightButton = this._renderRight();let statusBar = <View><StatusBar {...this.props.statusBar}/></View>;let titleView = this.props.titleView ? this.props.titleView :<Text style={[styles.titleStyle, this.props.titleLayoutStyle]}>{this.props.title}</Text>;let content = <View style={styles.content}>{leftButton}<View style={styles.titleView}>{titleView}</View>{rightButton}</View>;return (<ImageBackground source={this.props.showBackgroundIMG ? require('IMG/app_bar.png') : null}style={[styles.container, this.props.style]}>{statusBar}{content}</ImageBackground>)}_renderLeft() {let {leftButton, leftTextStyle, showLeft, navigation, onLeftClick, leftText, showleftImg} = this.props;if (!showLeft) {return null;}if (leftButton == null) {return (<TouchableOpacity onPress={() => {if (onLeftClick) {onLeftClick();} else {if (navigation) navigation.goBack()}}}><View style={styles.leftContainer}>{showleftImg ?<Image source={require('IMG/back.png')}style={{width: ScreenUtil.scaleSize(25),height: ScreenUtil.scaleSize(25)}}/>: null}<Text style={[styles.leftRightStyle, leftTextStyle]}>{leftText}</Text></View></TouchableOpacity>)}return leftButton;}_renderRight() {let {rightButton, rightTextStyle, showRight, onRightClick, rightText} = this.props;if (!showRight) {return null;}if (rightButton == null) {return (<TouchableOpacity onPress={() => {if (onRightClick) {onRightClick()}}}><View><Text style={[styles.leftRightStyle, rightTextStyle]}>{rightText}</Text></View></TouchableOpacity>)}return rightButton;}
}/*** //selector:这是你自己编写的一个函数。这个函数声明了你的组件需要整个 store 中的哪一部分数据作为自己的 props* 如果用不到redux这个方法可以删除* @param store* @returns {{color: *}}*/
function changeColor(store) {return {color: store.changeColorReducer.color}
}const styles = StyleSheet.create({container: {//backgroundColor:'green',width: ScreenUtil.screenW,height: ScreenUtil.scaleSize(58),justifyContent: 'center',},content: {justifyContent: 'space-between',flexDirection: 'row',alignItems: 'center',height: Platform.OS === 'ios' ? NAV_BAR_HEIGHT_IOS : NAV_BAR_HEIGHT_ANDROID,},titleView: {justifyContent: 'center',alignItems: 'center',position: 'absolute',left: 40,right: 40,top: 0,bottom: 0,},titleStyle: {fontSize: ScreenUtil.setSpText(18),color: 'white'},leftRightStyle: {color: 'white',fontSize: ScreenUtil.setSpText(14)},leftContainer: {marginLeft: ScreenUtil.scaleSize(17),flexDirection: 'row',alignItems: 'center'}
});// 包装 component ,注入 dispatch 和 state 到其默认的 connect(selector)(App) 中;
/*** 把这个组件用connect包裹住就能拿到store。注意export default已经拿到下面来了,上面的class前面的导出要删掉用redux的话需要第一种方法导出,已注释掉*/
//export default connect(changeColor)(withNavigation(NavigationBar));
//用不到redux推荐在定义类的时候直接导出,类定义为 export default class ...//当这个组件拿不到this.props.navigation时,可使用withNavigation
//withNavigation是一个更高阶的组件,它将导航道具传递给一个包装组件。
//当您无法直接将导航道具传递到组件时,或者在深度嵌套子节点的情况下不希望传递导航道具时,它非常有用。
//导出方式:export default withNavigation(NavigationBar);
export default NavigationBar;

关于withNavigation的使用

在页面使用:

常规页面插入
    render() {return (<View style={{flex: 1}}>{this._renderNav()}...</View>)}_renderNav() {return (<NavigationBar
                    title={'Main'}//showLeft={false}onLeftClick={()=>{alert('a')}}leftButton={()=><View><Text>自定义左侧按钮</Text></View>}statusBar={{barStyle: 'dark-content',backgroundColor: 'white',hidden: false,//true则隐藏}}/>)}
配合ReactNavigation使用
const RootStack = createStackNavigator({TaskList: {screen: TaskListPage},TaskDetails: {screen: TaskDetailsPage}
}, {navigationOptions: ({navigation}) => ({header: <NavigationBar title={navigation.state.routeName}showLeft={navigation.state.routeName !== 'TaskList'}navigation={navigation}/>})
});
属性:
PropTypeDefaultDescription
styleViewPropTypes.style-标题栏的样式
showBackgroundIMGbooltrue背景为一个图片
titlestring-标题使用的字符串
titleLayoutStyleViewPropTypes.style-标题文字的样式
titleViewPropTypes.element-替换标题文字的组件
leftButtonPropTypes.element-自定义左侧按钮
leftTextstring-左侧返回按钮的文字
leftTextStyleViewPropTypes.style-替换标题文字的组件
showLeftbooltrue是否显示返回按钮
leftImgsource的参数ImageSourcePropType左侧图片的地址
showleftImgbooltrue是否显示标题的后退按钮,需自行替换成其他Image
showLeftbooltrue是否显示返回按钮
onLeftClickfuncthis.props.navigation.goBack()左侧按钮的点击事件(默认为ReactNavigation的goback)
showRightboolfalse是否显示右侧按钮
rightTextstring更多右侧按钮的文字
rightImgsource的参数ImageSourcePropType右侧图片的地址,默认不传则不显示
rightTextStyleViewPropTypes.style-是否显示返回按钮
rightButtonPropTypes.element-自定义右侧按钮
onRightClickPropTypes.func-右侧按钮的点击事件
statusBar{ barStyle: PropTypes.oneOf([‘light-content’, ‘default’, ‘dark-content’]), hidden: PropTypes.bool, backgroundColor: PropTypes.string,}{ barStyle: ‘default’, hidden: false,}自定义状态栏

可以自己传入statusBar,默认值为:

 statusBar: {barStyle: 'default',hidden: false, //true则隐藏statusBar}

样式大概如此,颜色可以自己定义,
左侧按钮,中间文字,和右侧按钮都可以自己传入一个自己写的布局。
后续会完善功能和方法。

github地址

这篇关于ReactNative 自定义标题栏 ReactNative 自定义导航栏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1026172

相关文章

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor