前言

在刚开始学习React Native的时候,版本还是0.20,问题一大堆,Navigation这个问题更是很多,首先,是NavigationBar的问题,NavigationIOS有NavigationBar,Navigation却需要自定义一个,最后,我想了想,还是自定义一个view,岂不更好,现在新公司不用RN,我正好有点时间,就把自定义的NavigationBar分享给大家。好了少废话,上代码;

示例代码

// NavigationBar 导航条的自定义封装 
// create by 小广 
'use strict'; 
import React, { Component,PropTypes } from 'react'; 
import { 
 Image, 
 Text, 
 View, 
 Platform, 
 TouchableOpacity, 
} from 'react-native'; 
 
import styles from './NavigationBarStyle' 
 
// 导航条和状态栏的高度 
const STATUS_BAR_HEIGHT = 20 
const NAV_BAR_HEIGHT = 44 
 
export default class NavigationBar extends Component { 
 static defaultProps = { 
 title: 'title', 
 titleTextColor: '#383838', 
 titleViewFunc () {}, 
 barBGColor: '#f8f8f8', 
 barOpacity: 1, 
 barStyle: 0, 
 barBorderBottomColor: '#D4D4D4', 
 barBorderBottomWidth: 0.8, 
 statusbarShow: true, 
 leftItemTitle: '', 
 leftTextColor: '#383838', 
 leftItemFunc () {}, 
 rightItemTitle: '', 
 rightTextColor: '#383838', 
 rightItemFunc () {}, 
 //leftImageSource: require('./nav_back.png'), 
 }; 
 static propTypes = { 
 title: PropTypes.string,   // nav标题 
 titleTextColor: PropTypes.string, // nav标题颜色 
 titleView: PropTypes.node,  // nav自定义标题View(节点) 
 titleViewFunc: PropTypes.func, // nav的titleView点击事件 
 barBGColor: PropTypes.string, // Bar的背景颜色 
 barOpacity: PropTypes.number, // Bar的透明度 
 barStyle: PropTypes.number, // Bar的扩展属性,nav样式(暂未使用) 
 barBorderBottomColor: PropTypes.string, // Bar底部线的颜色 
 barBorderBottomWidth: PropTypes.number, // Bar底部线的宽度 
 statusbarShow: PropTypes.bool,  // 是否显示状态栏的20高度(默认true) 
 leftItemTitle: PropTypes.string, // 左按钮title 
 leftImageSource: PropTypes.node, // 左Item图片(source) 
 leftTextColor: PropTypes.string, // 左按钮标题颜色 
 leftItemFunc: PropTypes.func,  // 左Item事件 
 rightItemTitle: PropTypes.string, // 右按钮title 
 rightImageSource: PropTypes.node, // 右Item图片(source) 
 rightTextColor: PropTypes.string, // 右按钮标题颜色 
 rightItemFunc: PropTypes.func,  // 右Item事件 
 }; 
 
 render() { 
 // 判断左Item的类型 
 var onlyLeftIcon = false; // 是否只是图片 
 if (this.props.leftItemTitle && this.props.leftImageSource) { 
  onlyLeftIcon = true; 
 } else if (this.props.leftImageSource) { 
  onlyLeftIcon = true; 
 } 
 
 // 左侧图片title都没有的情况下 
 var noneLeft = false; 
 if (!(this.props.leftItemTitle.length > 0) && !(this.props.leftImageSource)) { 
  noneLeft = true; 
 } 
 
 // 判断是否自定义titleView 
 var hasTitleView = false; 
 if (this.props.title && this.props.titleView) { 
  hasTitleView = true; 
 } else if (this.props.titleView) { 
  hasTitleView = true; 
 } 
 
 // 判断右Item的类型 
 var onlyRightIcon = false; // 是否只是图片 
 if (this.props.rightItemTitle && this.props.rightImageSource) { 
  onlyRightIcon = true; 
 } else if (this.props.rightImageSource) { 
  onlyRightIcon = true; 
 } 
 
 // 右侧图片title都没有的情况下 
 var noneRight = false; 
 if (!(this.props.rightItemTitle.length > 0) && !(this.props.rightImageSource)) { 
  noneRight = true; 
 } 
 
 // 判断是否显示20状态栏高度 
 let showStatusbar = this.props.statusbarShow; 
 if (Platform.OS === 'android') { 
  // 安卓不显示 
  showStatusbar = false; 
 } 
 return ( 
  <View style={styles.nav_barView}> 
  <View style={[styles.nav_bar, 
   { 
   backgroundColor: this.props.barBGColor, 
   height: showStatusbar ? NAV_BAR_HEIGHT   STATUS_BAR_HEIGHT : NAV_BAR_HEIGHT, 
   opacity: this.props.barOpacity 
   }, 
   showStatusbar ? { paddingTop: STATUS_BAR_HEIGHT } : {}, this.props.barStyle]}> 
   <View style={styles.nav_ItemView}> 
   { // 左侧item 
    !noneLeft 
    ? <TouchableOpacity 
     style={styles.nav_leftItem} 
     onPress={this.props.leftItemFunc}> 
     { // 左侧是图片还是文字 
     onlyLeftIcon 
     ? <Image style={styles.nav_leftImage} 
        source={this.props.leftImageSource}/> 
     : <Text style={[styles.nav_leftTitle,{color: this.props.leftTextColor}]}> 
      {this.props.leftItemTitle} 
      </Text> 
     } 
    </TouchableOpacity> 
    : null 
   } 
   </View> 
   { 
   hasTitleView 
   ? <TouchableOpacity style={styles.nav_titleView} onPress={this.props.titleViewFunc}> 
    {this.props.titleView} 
    </TouchableOpacity> 
   : <View style={styles.nav_titleView}> 
    <Text style={[styles.nav_title,{color:this.props.titleTextColor}]}> 
     {this.props.title} 
    </Text> 
    </View> 
   } 
   <View style={styles.nav_ItemView}> 
   { // 右侧item 
    !noneRight 
    ? <TouchableOpacity 
     style={styles.nav_rightItem} 
     onPress={this.props.rightItemFunc}> 
     { // 右侧是图片还是文字 
     onlyRightIcon 
     ? <Image style={styles.nav_rightImage} 
        source={this.props.rightImageSource}/> 
     : <Text style={[styles.nav_rightTitle,{color: this.props.rightTextColor}]}> 
      {this.props.rightItemTitle} 
      </Text> 
     } 
    </TouchableOpacity> 
    : null 
   } 
   </View> 
  </View> 
  <View style={{height:this.props.barBorderBottomWidth,backgroundColor:this.props.barBorderBottomColor}}></View> 
  </View> 
 
 ); 
 } 
} 

css样式:

// NavigationBarStyle 导航条的样式 
// create by 小广 
'use strict'; 
import { 
 StyleSheet, 
} from 'react-native'; 
 
export default StyleSheet.create({ 
 // navBar 
 nav_barView:{ 
 justifyContent: 'center', 
 }, 
 nav_bar: { 
 //flex:1, 
 flex: 1, 
 flexDirection:'row', 
 justifyContent: 'center', 
 }, 
 
 // 标题纯title 
 nav_title: { 
 fontSize:17, 
 }, 
 
 // titleView 
 nav_titleView: { 
 flex: 1, 
 alignItems: 'center', 
 justifyContent: 'center', 
 }, 
 
 nav_ItemView:{ 
 width:80, 
 justifyContent: 'center', 
 }, 
 
 // 左Item 
 nav_leftItem: { 
 marginLeft:8, 
 flex:1, 
 justifyContent: 'center', 
 alignSelf: 'flex-start', 
 //backgroundColor:'#f00', 
 }, 
 
 // 左Item为title 
 nav_leftTitle: { 
  marginRight:5, 
  marginLeft:5, 
  fontSize: 14, 
 }, 
 
 // 左图片 
 nav_leftImage: { 
  margin:10, 
  resizeMode:'contain', 
 }, 
 
 // 右Item 
 nav_rightItem: { 
  marginRight:8, 
  flex:1, 
  justifyContent: 'center', 
  alignSelf: 'flex-end', 
  //backgroundColor:'#3393F2', 
 }, 
 
 // 右Item为title 
 nav_rightTitle: { 
  marginRight:5, 
  marginLeft:5, 
  fontSize: 14, 
 }, 
 
 // 右图片 
 nav_rightImage:{ 
  margin:10, 
  resizeMode:'contain', 
  //backgroundColor:'#f00', 
 }, 
 //resizeMode:'contain', 
}); 

用法:引入之后

import NavigationBar from '你的存放路径/NavigationBar.js'

class XGRNDemo extends Component { 
 
 _leftItemAction() { 
 console.log('左侧按钮点击了'); 
 } 
 
 _rightItemAction() { 
 console.log('右侧按钮点击了'); 
 } 
 
 render() { 
 return ( 
  <View style={styles.container}> 
  <NavigationBar 
   title='这个是标题' 
   leftImageSource={require('./nav_back.png')} 
   rightItemTitle='按钮' 
   rightTextColor='#3393F2' 
   leftItemFunc={this._leftItemAction.bind(this)} 
   rightItemFunc={this._rightItemAction.bind(this)}/> 
  <ScrollView style={styles.container} 
   automaticallyAdjustContentInsets={false} 
   keyboardShouldPersistTaps={true} 
   keyboardDismissMode='on-drag' 
   > 
   <Text style={styles.welcome}> 
   Welcome to React Native! 
   </Text> 
   <Text style={styles.instructions}> 
   To get started, edit index.ios.js 
   </Text> 
   <Text style={styles.instructions}> 
   Press Cmd R to reload,{'\n'} 
   Cmd D or shake for dev menu 
   </Text> 
  </ScrollView> 
  </View> 
 ); 
 } 
} 
 
const styles = StyleSheet.create({ 
 container: { 
 flex: 1, 
 backgroundColor: '#F5FCFF', 
 }, 
 welcome: { 
 fontSize: 20, 
 textAlign: 'center', 
 margin: 10, 
 }, 
 instructions: { 
 textAlign: 'center', 
 color: '#333333', 
 marginBottom: 5, 
 }, 
}); 

其中可以自定义的属性

title: PropTypes.string,   // nav标题 
titleTextColor: PropTypes.string, // nav标题颜色 
titleView: PropTypes.node,  // nav自定义标题View(节点) 
titleViewFunc: PropTypes.func, // nav的titleView点击事件 
barBGColor: PropTypes.string, // Bar的背景颜色 
barOpacity: PropTypes.number, // Bar的透明度 
barStyle: PropTypes.number, // Bar的扩展属性,nav样式(暂未使用) 
barBorderBottomColor: PropTypes.string, // Bar底部线的颜色 
barBorderBottomWidth: PropTypes.number, // Bar底部线的宽度 
statusbarShow: PropTypes.bool,  // 是否显示状态栏的20高度(默认true) 
leftItemTitle: PropTypes.string, // 左按钮title 
leftImageSource: PropTypes.node, // 左Item图片(source) 
leftTextColor: PropTypes.string, // 左按钮标题颜色 
leftItemFunc: PropTypes.func,  // 左Item事件 
rightItemTitle: PropTypes.string, // 右按钮title 
rightImageSource: PropTypes.node, // 右Item图片(source) 
rightTextColor: PropTypes.string, // 右按钮标题颜色 
rightItemFunc: PropTypes.func,  // 右Item事件 

效果如图:

ps:之前想上传到npm服务器,但是自己没搞成功,就这了吧..

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对Devmax的支持。

React Native学习教程之自定义NavigationBar详解的更多相关文章

  1. vue自定义加载指令v-loading占位图指令v-showimg

    这篇文章主要为大家介绍了vue自定义加载指令和v-loading占位图指令v-showimg的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  2. element-table如何实现自定义表格排序

    这篇文章主要介绍了element-table如何实现自定义表格排序,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. vue如何自定义地址设置@

    这篇文章主要介绍了vue如何自定义地址设置@,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  4. SpringBoot实现自定义事件的方法详解

    这篇文章将用实例来和大家介绍一下如何在SpringBoot中自定义事件来使用观察者模式。文中的示例代码讲解详细,对我们学习SpringBoot有一定的帮助,需要的可以参考一下

  5. React Native 中添加自定义字体的方法

    这篇文章主要介绍了如何在 React Native 中添加自定义字体,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android 自定义View手写签名并保存图片功能

    这篇文章主要介绍了Android 自定义View手写签名并保存图片功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值 ,需要的朋友可以参考下

  7. Spring Cloud超详细i讲解Feign自定义配置与使用

    这篇文章主要介绍了SpringCloud Feign自定义配置与使用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  8. 利用Android实现比较炫酷的自定义View

    自定义View、多线程、网络,被认为是Android开发者必须牢固掌握的最基础的三大基本功,这篇文章主要给大家介绍了关于如何利用Android实现比较炫酷的自定义View的相关资料,需要的朋友可以参考下

  9. Android自定义gridView仿头条频道拖动管理功能

    这篇文章主要介绍了Android自定义gridView仿头条频道拖动管理功能,本文通过实例代码效果图展示给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

  10. vue.js Table 组件自定义列宽实现核心方法

    这篇文章主要介绍了vue.js Table 组件自定义列宽实现核心方法,文围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部