NavigationBar.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import React,{Component} from 'react'
  2. import {ViewPropTypes,StyleSheet,Text,View,StatusBar,Platform,DeviceInfo} from 'react-native'
  3. import {PropTypes} from 'prop-types'
  4. const NAV_BAR_HEIGHT_IOS = 44;
  5. const NAV_BAR_HEIGHT_ANDROID = 50;
  6. const STATUS_BAR_HEIGHT = DeviceInfo.isIPhoneX_deprecated ? 0:20;
  7. const StatusBarShape = {//设置状态栏所接受的属性
  8. barStyle:PropTypes.oneOf(['light-content','default']),
  9. hidden:PropTypes.bool,
  10. backgroundColor:PropTypes.string,
  11. };
  12. export default class NavigationBar extends Component{
  13. //提供属性的类型检查
  14. static propTypes = {
  15. style:ViewPropTypes.style,
  16. title:PropTypes.string,
  17. titleView:PropTypes.element,
  18. titleLayoutStyle:ViewPropTypes.style,
  19. hide:PropTypes.bool,
  20. statusBar:PropTypes.shape(StatusBarShape),
  21. leftButton:PropTypes.element,
  22. rightButton:PropTypes.element,
  23. };
  24. //设置默认属性
  25. static defaultProps = {
  26. statusBar:{
  27. barStyle:'light-content',
  28. hidden:false,
  29. },
  30. };
  31. render() {
  32. let statusBar = !this.props.statusBar.hidden ?
  33. (<View style={styles.statusBar_bar}>
  34. <StatusBar {...this.props.statusBar} />
  35. </View>):null;
  36. let titleView = this.props.titleView ?
  37. this.props.titleView:(<Text ellipsizeMode="head" numberOfLines={1} style={styles.statusBar_title}>{this.props.title}</Text>);
  38. let content = this.props.hide?null:
  39. (<View style={styles.statusBar_navBar}>
  40. {this.getButtonElement(this.props.leftButton)}
  41. <View style={[styles.statusBar_navBarTitleContainer,this.props.titleLayoutStyle]}>
  42. {titleView}
  43. </View>
  44. {this.getButtonElement(this.props.rightButton)}
  45. </View>);
  46. return (<View style={[styles.container,this.props.style]}>
  47. {statusBar}
  48. {content}
  49. </View>)
  50. }
  51. getButtonElement(data){
  52. return (<View style={styles.navBarButton}>
  53. {data ? data : null}
  54. </View>)
  55. }
  56. }
  57. const styles = StyleSheet.create({
  58. container:{
  59. backgroundColor:'#2196f3',
  60. },
  61. statusBar_bar:{
  62. height:Platform.OS==='ios'?STATUS_BAR_HEIGHT:0,
  63. },
  64. statusBar_title:{
  65. fontSize:18,
  66. color:'white',
  67. },
  68. statusBar_navBar:{
  69. flexDirection:'row',
  70. alignItems: 'center',
  71. justifyContent:'space-between',
  72. height:Platform.OS === 'ios'?NAV_BAR_HEIGHT_IOS:NAV_BAR_HEIGHT_ANDROID,
  73. },
  74. statusBar_navBarTitleContainer:{
  75. alignItems:'center',
  76. justifyContent: 'center',
  77. position:'absolute',
  78. left:40,
  79. right:40,
  80. top:0,
  81. bottom:0,
  82. },
  83. navBarButton:{
  84. alignItems:'center',
  85. },
  86. });