React Native学习之自定义NavigationBar

来源:互联网 发布:迅雷快鸟mac版 编辑:程序博客网 时间:2024/06/09 16:06

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // NavigationBar 导航条的自定义封装  
  2. // create by 小广  
  3. 'use strict';  
  4. import React, { Component,PropTypes } from 'react';  
  5. import {  
  6.   Image,  
  7.   Text,  
  8.   View,  
  9.   Platform,  
  10.   TouchableOpacity,  
  11. } from 'react-native';  
  12.   
  13. import styles from './NavigationBarStyle'  
  14.   
  15. // 导航条和状态栏的高度  
  16. const STATUS_BAR_HEIGHT = 20  
  17. const NAV_BAR_HEIGHT = 44  
  18.   
  19. export default class NavigationBar extends Component {  
  20.   static defaultProps = {  
  21.     title: 'title',  
  22.     titleTextColor: '#383838',  
  23.     titleViewFunc () {},  
  24.     barBGColor: '#f8f8f8',  
  25.     barOpacity: 1,  
  26.     barStyle: 0,  
  27.     barBorderBottomColor: '#D4D4D4',  
  28.     barBorderBottomWidth: 0.8,  
  29.     statusbarShow: true,  
  30.     leftItemTitle: '',  
  31.     leftTextColor: '#383838',  
  32.     leftItemFunc () {},  
  33.     rightItemTitle: '',  
  34.     rightTextColor: '#383838',  
  35.     rightItemFunc () {},  
  36.     //leftImageSource: require('./nav_back.png'),  
  37.   };  
  38.   static propTypes = {  
  39.     title: PropTypes.string,          // nav标题  
  40.     titleTextColor: PropTypes.string, // nav标题颜色  
  41.     titleView: PropTypes.node,        // nav自定义标题View(节点)  
  42.     titleViewFunc: PropTypes.func,    // nav的titleView点击事件  
  43.     barBGColor: PropTypes.string, // Bar的背景颜色  
  44.     barOpacity: PropTypes.number, // Bar的透明度  
  45.     barStyle: PropTypes.number,   // Bar的扩展属性,nav样式(暂未使用)  
  46.     barBorderBottomColor: PropTypes.string,  // Bar底部线的颜色  
  47.     barBorderBottomWidth: PropTypes.number,  // Bar底部线的宽度  
  48.     statusbarShow: PropTypes.bool,     // 是否显示状态栏的20高度(默认true)  
  49.     leftItemTitle: PropTypes.string,   // 左按钮title  
  50.     leftImageSource: PropTypes.node,   // 左Item图片(source)  
  51.     leftTextColor: PropTypes.string,   // 左按钮标题颜色  
  52.     leftItemFunc: PropTypes.func,      // 左Item事件  
  53.     rightItemTitle: PropTypes.string,  // 右按钮title  
  54.     rightImageSource: PropTypes.node,  // 右Item图片(source)  
  55.     rightTextColor: PropTypes.string,  // 右按钮标题颜色  
  56.     rightItemFunc: PropTypes.func,     // 右Item事件  
  57.   };  
  58.   
  59.   render() {  
  60.     // 判断左Item的类型  
  61.     var onlyLeftIcon = false; // 是否只是图片  
  62.     if (this.props.leftItemTitle && this.props.leftImageSource) {  
  63.         onlyLeftIcon = true;  
  64.     } else if (this.props.leftImageSource) {  
  65.       onlyLeftIcon = true;  
  66.     }  
  67.   
  68.     // 左侧图片title都没有的情况下  
  69.     var noneLeft = false;  
  70.     if (!(this.props.leftItemTitle.length > 0) && !(this.props.leftImageSource)) {  
  71.       noneLeft = true;  
  72.     }  
  73.   
  74.     // 判断是否自定义titleView  
  75.     var hasTitleView = false;  
  76.     if (this.props.title && this.props.titleView) {  
  77.         hasTitleView = true;  
  78.     } else if (this.props.titleView) {  
  79.       hasTitleView = true;  
  80.     }  
  81.   
  82.     // 判断右Item的类型  
  83.     var onlyRightIcon = false; // 是否只是图片  
  84.     if (this.props.rightItemTitle && this.props.rightImageSource) {  
  85.         onlyRightIcon = true;  
  86.     } else if (this.props.rightImageSource) {  
  87.       onlyRightIcon = true;  
  88.     }  
  89.   
  90.     // 右侧图片title都没有的情况下  
  91.     var noneRight = false;  
  92.     if (!(this.props.rightItemTitle.length > 0) && !(this.props.rightImageSource)) {  
  93.       noneRight = true;  
  94.     }  
  95.   
  96.     // 判断是否显示20状态栏高度  
  97.     let showStatusbar = this.props.statusbarShow;  
  98.     if (Platform.OS === 'android') {  
  99.       // 安卓不显示  
  100.       showStatusbar = false;  
  101.     }  
  102.     return (  
  103.       <View style={styles.nav_barView}>  
  104.         <View style={[styles.nav_bar,  
  105.           {  
  106.             backgroundColor: this.props.barBGColor,  
  107.             height: showStatusbar ? NAV_BAR_HEIGHT + STATUS_BAR_HEIGHT : NAV_BAR_HEIGHT,  
  108.             opacity: this.props.barOpacity  
  109.           },  
  110.           showStatusbar ? { paddingTop: STATUS_BAR_HEIGHT } : {}, this.props.barStyle]}>  
  111.           <View style={styles.nav_ItemView}>  
  112.             { // 左侧item  
  113.               !noneLeft  
  114.               ? <TouchableOpacity  
  115.                  style={styles.nav_leftItem}  
  116.                  onPress={this.props.leftItemFunc}>  
  117.                  { // 左侧是图片还是文字  
  118.                    onlyLeftIcon  
  119.                    ? <Image style={styles.nav_leftImage}  
  120.                               source={this.props.leftImageSource}/>  
  121.                    : <Text style={[styles.nav_leftTitle,{color: this.props.leftTextColor}]}>  
  122.                        {this.props.leftItemTitle}  
  123.                      </Text>  
  124.                  }  
  125.                </TouchableOpacity>  
  126.               : null  
  127.             }  
  128.           </View>  
  129.           {  
  130.             hasTitleView  
  131.             ? <TouchableOpacity style={styles.nav_titleView} onPress={this.props.titleViewFunc}>  
  132.                 {this.props.titleView}  
  133.               </TouchableOpacity>  
  134.             : <View style={styles.nav_titleView}>  
  135.                 <Text style={[styles.nav_title,{color:this.props.titleTextColor}]}>  
  136.                  {this.props.title}  
  137.                 </Text>  
  138.               </View>  
  139.           }  
  140.           <View style={styles.nav_ItemView}>  
  141.             { // 右侧item  
  142.               !noneRight  
  143.               ? <TouchableOpacity  
  144.                  style={styles.nav_rightItem}  
  145.                  onPress={this.props.rightItemFunc}>  
  146.                  { // 右侧是图片还是文字  
  147.                    onlyRightIcon  
  148.                    ? <Image style={styles.nav_rightImage}  
  149.                               source={this.props.rightImageSource}/>  
  150.                    : <Text style={[styles.nav_rightTitle,{color: this.props.rightTextColor}]}>  
  151.                       {this.props.rightItemTitle}  
  152.                      </Text>  
  153.                  }  
  154.               </TouchableOpacity>  
  155.               : null  
  156.             }  
  157.           </View>  
  158.         </View>  
  159.         <View style={{height:this.props.barBorderBottomWidth,backgroundColor:this.props.barBorderBottomColor}}></View>  
  160.       </View>  
  161.   
  162.     );  
  163.   }  
  164. }  

css样式:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // NavigationBarStyle 导航条的样式  
  2. // create by 小广  
  3. 'use strict';  
  4. import {  
  5.   StyleSheet,  
  6. } from 'react-native';  
  7.   
  8. export default  StyleSheet.create({  
  9.   // navBar  
  10.   nav_barView:{  
  11.     justifyContent: 'center',  
  12.   },  
  13.   nav_bar: {  
  14.     //flex:1,  
  15.     flex: 1,  
  16.     flexDirection:'row',  
  17.     justifyContent: 'center',  
  18.   },  
  19.   
  20.   // 标题纯title  
  21.   nav_title: {  
  22.     fontSize:17,  
  23.   },  
  24.   
  25.   // titleView  
  26.   nav_titleView: {  
  27.     flex: 1,  
  28.     alignItems: 'center',  
  29.     justifyContent: 'center',  
  30.   },  
  31.   
  32.   nav_ItemView:{  
  33.     width:80,  
  34.     justifyContent: 'center',  
  35.   },  
  36.   
  37.   // 左Item  
  38.   nav_leftItem: {  
  39.     marginLeft:8,  
  40.     flex:1,  
  41.     justifyContent: 'center',  
  42.     alignSelf: 'flex-start',  
  43.     //backgroundColor:'#f00',  
  44.   },  
  45.   
  46.   // 左Item为title  
  47.    nav_leftTitle: {  
  48.      marginRight:5,  
  49.       marginLeft:5,  
  50.       fontSize: 14,  
  51.    },  
  52.   
  53.    // 左图片  
  54.    nav_leftImage: {  
  55.      margin:10,  
  56.      resizeMode:'contain',  
  57.    },  
  58.   
  59.    // 右Item  
  60.    nav_rightItem: {  
  61.       marginRight:8,  
  62.       flex:1,  
  63.       justifyContent: 'center',  
  64.       alignSelf: 'flex-end',  
  65.       //backgroundColor:'#3393F2',  
  66.    },  
  67.   
  68.    // 右Item为title  
  69.     nav_rightTitle: {  
  70.       marginRight:5,  
  71.       marginLeft:5,  
  72.       fontSize: 14,  
  73.     },  
  74.   
  75.     // 右图片  
  76.     nav_rightImage:{  
  77.       margin:10,  
  78.       resizeMode:'contain',  
  79.       //backgroundColor:'#f00',  
  80.     },  
  81.     //resizeMode:'contain',  
  82. });  

用法:引入之后

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. class XGRNDemo extends Component {  
  2.   
  3.   _leftItemAction() {  
  4.     console.log('左侧按钮点击了');  
  5.   }  
  6.   
  7.   _rightItemAction() {  
  8.     console.log('右侧按钮点击了');  
  9.   }  
  10.   
  11.   render() {  
  12.     return (  
  13.       <View style={styles.container}>  
  14.         <NavigationBar  
  15.             title='这个是标题'  
  16.             leftImageSource={require('./nav_back.png')}  
  17.             rightItemTitle='按钮'  
  18.             rightTextColor='#3393F2'  
  19.             leftItemFunc={this._leftItemAction.bind(this)}  
  20.             rightItemFunc={this._rightItemAction.bind(this)}/>  
  21.         <ScrollView style={styles.container}  
  22.           automaticallyAdjustContentInsets={false}  
  23.           keyboardShouldPersistTaps={true}  
  24.           keyboardDismissMode='on-drag'  
  25.           >  
  26.           <Text style={styles.welcome}>  
  27.             Welcome to React Native!  
  28.           </Text>  
  29.           <Text style={styles.instructions}>  
  30.             To get started, edit index.ios.js  
  31.           </Text>  
  32.           <Text style={styles.instructions}>  
  33.             Press Cmd+R to reload,{'\n'}  
  34.             Cmd+D or shake for dev menu  
  35.           </Text>  
  36.         </ScrollView>  
  37.       </View>  
  38.     );  
  39.   }  
  40. }  
  41.   
  42. const styles = StyleSheet.create({  
  43.   container: {  
  44.     flex: 1,  
  45.     backgroundColor: '#F5FCFF',  
  46.   },  
  47.   welcome: {  
  48.     fontSize: 20,  
  49.     textAlign: 'center',  
  50.     margin: 10,  
  51.   },  
  52.   instructions: {  
  53.     textAlign: 'center',  
  54.     color: '#333333',  
  55.     marginBottom: 5,  
  56.   },  
  57. });  

其中可以自定义的属性

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. title: PropTypes.string,          // nav标题  
  2. titleTextColor: PropTypes.string, // nav标题颜色  
  3. titleView: PropTypes.node,        // nav自定义标题View(节点)  
  4. titleViewFunc: PropTypes.func,    // nav的titleView点击事件  
  5. barBGColor: PropTypes.string, // Bar的背景颜色  
  6. barOpacity: PropTypes.number, // Bar的透明度  
  7. barStyle: PropTypes.number,   // Bar的扩展属性,nav样式(暂未使用)  
  8. barBorderBottomColor: PropTypes.string,  // Bar底部线的颜色  
  9. barBorderBottomWidth: PropTypes.number,  // Bar底部线的宽度  
  10. statusbarShow: PropTypes.bool,     // 是否显示状态栏的20高度(默认true)  
  11. leftItemTitle: PropTypes.string,   // 左按钮title  
  12. leftImageSource: PropTypes.node,   // 左Item图片(source)  
  13. leftTextColor: PropTypes.string,   // 左按钮标题颜色  
  14. leftItemFunc: PropTypes.func,      // 左Item事件  
  15. rightItemTitle: PropTypes.string,  // 右按钮title  
  16. rightImageSource: PropTypes.node,  // 右Item图片(source)  
  17. rightTextColor: PropTypes.string,  // 右按钮标题颜色  
  18. rightItemFunc: PropTypes.func,     // 右Item事件  


效果如图:

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

0
0 0
原创粉丝点击