最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

React Native學(xué)習(xí)教程之自定義NavigationBar詳解

 更新時(shí)間:2017年10月18日 09:47:38   作者:開發(fā)仔XG  
這篇文章主要給大家介紹了關(guān)于React Native學(xué)習(xí)教程之自定義NavigationBar的相關(guān)資料,文中通過是示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

在剛開始學(xué)習(xí)React Native的時(shí)候,版本還是0.20,問題一大堆,Navigation這個(gè)問題更是很多,首先,是NavigationBar的問題,NavigationIOS有NavigationBar,Navigation卻需要自定義一個(gè),最后,我想了想,還是自定義一個(gè)view,豈不更好,現(xiàn)在新公司不用RN,我正好有點(diǎn)時(shí)間,就把自定義的NavigationBar分享給大家。好了少廢話,上代碼;

示例代碼

// NavigationBar 導(dǎo)航條的自定義封裝 
// create by 小廣 
'use strict'; 
import React, { Component,PropTypes } from 'react'; 
import { 
 Image, 
 Text, 
 View, 
 Platform, 
 TouchableOpacity, 
} from 'react-native'; 
 
import styles from './NavigationBarStyle' 
 
// 導(dǎo)航條和狀態(tài)欄的高度 
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標(biāo)題 
 titleTextColor: PropTypes.string, // nav標(biāo)題顏色 
 titleView: PropTypes.node,  // nav自定義標(biāo)題View(節(jié)點(diǎn)) 
 titleViewFunc: PropTypes.func, // nav的titleView點(diǎn)擊事件 
 barBGColor: PropTypes.string, // Bar的背景顏色 
 barOpacity: PropTypes.number, // Bar的透明度 
 barStyle: PropTypes.number, // Bar的擴(kuò)展屬性,nav樣式(暫未使用) 
 barBorderBottomColor: PropTypes.string, // Bar底部線的顏色 
 barBorderBottomWidth: PropTypes.number, // Bar底部線的寬度 
 statusbarShow: PropTypes.bool,  // 是否顯示狀態(tài)欄的20高度(默認(rèn)true) 
 leftItemTitle: PropTypes.string, // 左按鈕title 
 leftImageSource: PropTypes.node, // 左Item圖片(source) 
 leftTextColor: PropTypes.string, // 左按鈕標(biāo)題顏色 
 leftItemFunc: PropTypes.func,  // 左Item事件 
 rightItemTitle: PropTypes.string, // 右按鈕title 
 rightImageSource: PropTypes.node, // 右Item圖片(source) 
 rightTextColor: PropTypes.string, // 右按鈕標(biāo)題顏色 
 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; 
 } 
 
 // 左側(cè)圖片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; 
 } 
 
 // 右側(cè)圖片title都沒有的情況下 
 var noneRight = false; 
 if (!(this.props.rightItemTitle.length > 0) && !(this.props.rightImageSource)) { 
  noneRight = true; 
 } 
 
 // 判斷是否顯示20狀態(tài)欄高度 
 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}> 
   { // 左側(cè)item 
    !noneLeft 
    ? <TouchableOpacity 
     style={styles.nav_leftItem} 
     onPress={this.props.leftItemFunc}> 
     { // 左側(cè)是圖片還是文字 
     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}> 
   { // 右側(cè)item 
    !noneRight 
    ? <TouchableOpacity 
     style={styles.nav_rightItem} 
     onPress={this.props.rightItemFunc}> 
     { // 右側(cè)是圖片還是文字 
     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 導(dǎo)航條的樣式 
// 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', 
 }, 
 
 // 標(biāo)題純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('左側(cè)按鈕點(diǎn)擊了'); 
 } 
 
 _rightItemAction() { 
 console.log('右側(cè)按鈕點(diǎn)擊了'); 
 } 
 
 render() { 
 return ( 
  <View style={styles.container}> 
  <NavigationBar 
   title='這個(gè)是標(biāo)題' 
   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標(biāo)題 
titleTextColor: PropTypes.string, // nav標(biāo)題顏色 
titleView: PropTypes.node,  // nav自定義標(biāo)題View(節(jié)點(diǎn)) 
titleViewFunc: PropTypes.func, // nav的titleView點(diǎn)擊事件 
barBGColor: PropTypes.string, // Bar的背景顏色 
barOpacity: PropTypes.number, // Bar的透明度 
barStyle: PropTypes.number, // Bar的擴(kuò)展屬性,nav樣式(暫未使用) 
barBorderBottomColor: PropTypes.string, // Bar底部線的顏色 
barBorderBottomWidth: PropTypes.number, // Bar底部線的寬度 
statusbarShow: PropTypes.bool,  // 是否顯示狀態(tài)欄的20高度(默認(rèn)true) 
leftItemTitle: PropTypes.string, // 左按鈕title 
leftImageSource: PropTypes.node, // 左Item圖片(source) 
leftTextColor: PropTypes.string, // 左按鈕標(biāo)題顏色 
leftItemFunc: PropTypes.func,  // 左Item事件 
rightItemTitle: PropTypes.string, // 右按鈕title 
rightImageSource: PropTypes.node, // 右Item圖片(source) 
rightTextColor: PropTypes.string, // 右按鈕標(biāo)題顏色 
rightItemFunc: PropTypes.func,  // 右Item事件 

效果如圖:

ps:之前想上傳到npm服務(wù)器,但是自己沒搞成功,就這了吧..

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • 詳解iOS社會(huì)化分享集成

    詳解iOS社會(huì)化分享集成

    這篇文章主要介紹了詳解iOS社會(huì)化分享集成的相關(guān)知識(shí)點(diǎn)以及實(shí)現(xiàn)的代碼效果講述,有興趣的朋友參考下。
    2018-02-02
  • iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實(shí)例

    iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實(shí)例

    下面小編就為大家分享一篇iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS開發(fā)之銀行卡號(hào)識(shí)別

    iOS開發(fā)之銀行卡號(hào)識(shí)別

    本文給大家分享ios開發(fā)之銀行卡號(hào)識(shí)別功能,思路明確,需要的朋友參考下吧
    2016-12-12
  • IOS自定義UIButton九宮格效果

    IOS自定義UIButton九宮格效果

    這篇文章主要介紹了IOS自定義UIButton九宮格效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • iOS中的通知機(jī)制

    iOS中的通知機(jī)制

    網(wǎng)上經(jīng)常說iOS的通知機(jī)制是使用了觀察者模式,里面有兩個(gè)角色,其一是poster(發(fā)送者),另一個(gè)是observer(接受信息的訂閱者)。接下來通過本文給大家介紹iOS中的通知機(jī)制,感興趣的朋友一起學(xué)習(xí)吧
    2016-04-04
  • iOS 簡單的操作桿旋轉(zhuǎn)實(shí)現(xiàn)示例詳解

    iOS 簡單的操作桿旋轉(zhuǎn)實(shí)現(xiàn)示例詳解

    這篇文章主要為大家介紹了iOS 簡單的操作桿旋轉(zhuǎn)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • iOS 10 和Xcode8 一起 創(chuàng)建 Siri 功能步驟詳解(OC寫的 )

    iOS 10 和Xcode8 一起 創(chuàng)建 Siri 功能步驟詳解(OC寫的 )

    這篇文章主要介紹了iOS 10 和Xcode8 一起 創(chuàng)建 Siri 功能(OC寫的 ),本文分步驟給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2017-12-12
  • iOS將地址解析成經(jīng)緯度的方法

    iOS將地址解析成經(jīng)緯度的方法

    這篇文章主要為大家詳細(xì)介紹了iOS將地址解析成經(jīng)緯度的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • iOS開發(fā)教程之UIRefreshControl使用的踩坑指南

    iOS開發(fā)教程之UIRefreshControl使用的踩坑指南

    UIRefreshControl是iOS6自帶的UITableView下拉刷新控件。下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)教程之UIRefreshControl使用的踩坑指南,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • IOS設(shè)備上給body綁定click事件不生效的原因及解決辦法

    IOS設(shè)備上給body綁定click事件不生效的原因及解決辦法

    最近在做移動(dòng)端的項(xiàng)目,在ios上對(duì)body綁定click事件實(shí)現(xiàn)事件代理冒泡至某些元素上不生效,怎么回事,如何解決呢?今天小編給大家?guī)砹薎OS設(shè)備上給body綁定click事件不生效的原因及解決辦法,一起看看吧
    2016-09-09

最新評(píng)論

广汉市| 龙州县| 永福县| 新平| 德惠市| 徐州市| 拜泉县| 江西省| 石台县| 屏南县| 保亭| 林口县| 松桃| 玉门市| 威远县| 砀山县| 印江| 大渡口区| 布尔津县| 昭觉县| 磴口县| 弥勒县| 临江市| 南宁市| 秦安县| 区。| 灯塔市| 上思县| 临猗县| 应城市| 瓮安县| 景东| 多伦县| 金山区| 武陟县| 疏勒县| 敖汉旗| 达拉特旗| 遂昌县| 株洲市| 德庆县|