React Native 自定义导航栏随滚动渐变

实现效果预览:

代码实现:

1、定义导航栏 NavPage.js

import React, {Component} from 'react';
import {View, Text, Image, StyleSheet, TouchableOpacity, Platform, Dimensions} from 'react-native'; /**
* 自定义导航栏
*/
let height = (Platform.OS === 'ios' ? : ) + export default class NavPage extends Component { static defaultProps = {
title: 'title',
}; render() {
let {title} = this.props;
return (
<View style={styles.container}>
<TouchableOpacity style={styles.item} onPress={() => {
alert('返回')
}}>
<Image style={styles.icon} source={require('./arrow.png')}/>
</TouchableOpacity> <View style={{alignItems: 'center', flex: }}>
<Text style={{color: '#FFF', fontSize: }}>{title}</Text>
</View>
<TouchableOpacity style={styles.item} onPress={() => {
alert('更多')
}}>
<Image style={[styles.icon, {width: , height: }]} source={require('./more.png')}/>
</TouchableOpacity> </View>
);
}
} const styles = StyleSheet.create({
container: {
width: Dimensions.get('window').width,
height: height,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingTop: Platform.OS === 'ios' ? : ,
paddingHorizontal: ,
position: 'absolute',
},
icon: {
width: ,
height: ,
color: "white",
},
item: {
height: ,
width: ,
justifyContent: 'center',
alignItems: 'center'
}
});

调用实现:

import React, {Component} from 'react'
import {
StyleSheet,
Text,
View,
ListView,
Image,
Dimensions,
TextInput
} from 'react-native'
import NavPage from "./NavPage"; const {width} = Dimensions.get('window') export default class TestMyNav extends Component<{}> { constructor(props) {
super(props)
this.navBar = null
this.renderRow = this.renderRow.bind(this)
this.renderHeader = this.renderHeader.bind(this)
this.state = {
dataSource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
}
} renderRow(rowData, sectionId, rowId) {
return (
<View
style={{height: , justifyContent: 'center', borderWidth: , borderColor: 'red'}}
key={rowId}>
<Text style={{marginHorizontal: }}>{`这时第:${rowId}行`}</Text>
</View>
)
} renderHeader() {
return (
<View>
<Image style={{height: 200, width: width}}
source={{uri: 'https://upload.jianshu.io/users/upload_avatars/2174847/35584aef-dcac-46c0-9280-67a3b1ebb2c9.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/96/h/96'}}
resizeMode={'cover'}/>
</View>
)
} _onScroll(event) {
let y = event.nativeEvent.contentOffset.y
let opacityPercent = y / 200
if (y < 200) {
this.navBar.setNativeProps({
style: {opacity: opacityPercent}
})
} else {
this.navBar.setNativeProps({
style: {opacity: 1}
})
}
} render() {
let dataSource = this.state.dataSource.cloneWithRows([, , , , , , , , , ])
return (
<View style={styles.container}>
<ListView
onScroll={this._onScroll.bind(this)}
bounces={false}
dataSource={dataSource}
renderRow={this.renderRow}
renderHeader={this.renderHeader}/> <View
ref={ref => this.navBar = ref}
style={[styles.navBar, {opacity: 0}]}>
<NavPage title={'详情页'}/>
</View>
</View>
)
}
} const styles = StyleSheet.create({
container: {
flex: ,
backgroundColor: '#F5FCFF',
},
navBar: {
height: ,
width: width,
position: 'absolute',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FA0016',
},
navContent: {
marginTop: ,
height: ,
width: width,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal:
},
searchBar: {
justifyContent: 'center',
paddingHorizontal: ,
borderTopLeftRadius: ,
borderBottomLeftRadius: ,
borderTopRightRadius: ,
borderBottomRightRadius: ,
flex: ,
height: ,
backgroundColor: 'white',
marginHorizontal:
}
})

红色部分为核心代码

参考:

https://github.com/guangqiang-liu/react-native-gradientNavigationBarDemo

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/11020748.html

转载请著名出处!谢谢~~

[RN] React Native 自定义导航栏随滚动渐变的更多相关文章

  1. React Native自定义导航栏

    之前我们学习了可触摸组件和页面导航的使用的使用: 从零学React Native之09可触摸组件 - 从零学React Native之03页面导航 - 经过之前的学习, 我们可以完成一个自定义导航栏了 ...

  2. [RN] React Native 自定义 底部 弹出 选择框 实现

    React Native 自定义 底部选择框 实现 效果如图所示: 实现方法: 一.组件封装 CustomAlertDialog.js import React, {Component} from ' ...

  3. React Native自定义导航条

    Navigator和NavigatorIOS 在开发中,需要实现多个界面的切换,这时候就需要一个导航控制器来进行各种效果的切换.在React Native中RN为我们提供了两个组件:Navigator ...

  4. React Native 之导航栏

    一定要参考官网: https://reactnavigation.org/docs/en/getting-started.html 代码来自慕课网:https://www.imooc.com/cour ...

  5. React Native 底部导航栏

    首先安装:npm install react-native-tab-navigator   然后再引入文件中    import TabNavigator from 'react-native-tab ...

  6. iOS个人中心渐变动画、微信对话框、标签选择器、自定义导航栏、短信验证输入框等源码

    iOS精选源码 简单的个人中心页面-自定义导航栏并予以渐变动画 程序员取悦女票的正确姿势---Tip1(iOS美容篇) iOS 前台重启应用和清除角标的问题 微信原生提醒对话框3.0 JHLikeBu ...

  7. iOS:自定义导航栏,随着tableView滚动显示和隐藏

    自定义导航栏,随着tableView滚动显示和隐藏 一.介绍 自定义导航栏是APP中很常用的一个功能,通过自定义可以灵活的实现动画隐藏和显示效果.虽然处理系统的导航栏也可以实现,但是这个是有弊端的,因 ...

  8. Taro多端自定义导航栏Navbar+Tabbar实例

    运用Taro实现多端导航栏/tabbar实例 (H5 + 小程序 + React Native) 最近一直在捣鼓taro开发,虽说官网介绍支持编译到多端,但是网上大多数实例都是H5.小程序,很少有支持 ...

  9. ios7以上自定义导航栏标题的字体大小及颜色的方法

    自定义导航栏的字体和颜色,只需要自定义一个lable,然后将lable添加到导航栏的titleview中就可以了 代码如下 UILabel *label = [[UILabel alloc] init ...

随机推荐

  1. Debug 路漫漫-11:Python: TypeError: 'generator' object is not subscriptable

    调试程序,出现以下错误: Python: TypeError: 'generator' object is not subscriptable “在Python中,这种一边循环一边计算的机制,称为生成 ...

  2. 《Game Programming Patterns》游戏设计模式

    转载自:https://blog.csdn.net/poem_qianmo/article/details/52505170 https://blog.csdn.net/poem_qianmo/art ...

  3. 管理ceph缓存池

    目录 缓存池简介 缓存池原理 缓存池的工作模式 配置缓存池 1. 创建一个缓存池 2. 设置缓存层 3. 缓存层相关参数说明 4. 测试缓存池 删除缓存池 1. 删除read-only缓存池 2. 删 ...

  4. Java解压和压缩带密码的zip或rar文件(下载压缩文件中的选中文件、向压缩文件中新增、删除文件)

    JAVA 实现在线浏览管理zip和rar的工具类 (有密码及无密码的)以及下载压缩文件中的选中文件(向压缩文件中新增.删除文件) 这是之前的版本 JAVA 解压压缩包中指定文件或实现压缩文件的预览及下 ...

  5. 如何在Quartus II中查看RTL原理图

    整个工程代码编写并且编译完成之后,标题栏选择Tools→Netlist Viewers→RTL Viewer即可

  6. 【模板整合计划】DP动态规划

    [模板整合计划]DP动态规划 一:[背包] 1.[01背包] 采药 \([P1048]\) #include<algorithm> #include<cstdio> int T ...

  7. 《 .NET并发编程实战》阅读指南 - 第6章

    先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.

  8. C# ??(两个问号)的表达式使用详解

    今天有人问我C#中两个问号是什么意思,怎么使用,于是乎有了这篇随笔 有时候我们需要判断某个对象是否为null,一般的做法是 if(x=null){....} 若想让自己的代码更简洁,可以这样写: st ...

  9. 代码语法高亮踩坑-原理,问题, PRE元素及htmlentity

    语法高亮库基础原理 在研究使用能够在web页面上代码语法高显的解决方案时,发现有很多现成的开源库.比较中意的有prism.js,highlightjs.他们的原理基本上核心就两点: 1. 利用html ...

  10. JS删除指定下标的元素

    在开发过程中,有时我们需要删除数组中某一下标的元素.JAVA中ArrayList有remove函数.但是在JavaScript中没有直接的删除方法.我们可以利用splice来实现.Array.spli ...