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. 使用STS加入controller注解后编写程序无法自动提示

    1.加入@Controller注解后编写程序无法自动提示,去掉了@Controller注解后就可以了! ​ ​ 2.解决方案:将@Controller替换为@RestController后,可以完美的 ...

  2. 我自己收藏的 Windows 上好用的软件

    已经在使用的工具就不会列出来了. 1. 截图 - Snipaste 截图在我们的生活中,可以算的上是非常频繁的操作了,但是很多人是不是都在使用腾讯聊天软件的聊天截图功能,或许是没有一款称心的.安全的截 ...

  3. WPF ResourceDictionary XAML资源 c#代码 获取与遍历

    使用C#代码来获取XAML资源,除去正常的FindResource.而且是能查询到资源的对象. 说实话还是很麻烦的. 比如说我现在有一堆静态资源放在xaml的资源中,我想通过绑定的方式来获取. 好比是 ...

  4. MongoDB 学习笔记 ---创建用户

    MongoDB安装好了之后,开始学习常用命令 首先,运行MongoDB, 记住,先不用带参数--auth /usr/local/mongodb/bin/mongod -dbpath=/usr/loca ...

  5. 【leetcode-449】序列化和反序列化二叉搜索树

    序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法来序列化和反序列化二叉搜索树. 对序列 ...

  6. dedecms5.7执行PHP代码的用法

    dedecms5.7执行PHP代码的用法 {dede:php} echo 'test'; {/dede:php}

  7. JAVA设计模式工厂模式

    工厂模式: – 实现了创建者和调用者的分离. – 详细分类: • 简单工厂模式 • 工厂方法模式 • 抽象工厂模式• 面向对象设计的基本原则: – OCP(开闭原则,Open-Closed Princ ...

  8. vue中操作localstorage

    首先在子组件将localstorage方法进行封装 在父组件中对其进行引用 将输入的值存入到定义的searchHistory数组中,存储localstorage需要传两个参数,变量名为searchHi ...

  9. Generate a document using docxtemplater

    生成word文档,更新word内容 http://javascript-ninja.fr/docxtemplater/v1/examples/demo.html   https://docxtempl ...

  10. 关于IOS AFNetWorking内存泄漏的问题

    之前项目中用Instruments的leaks检测APP,结果发现APP的网络请求会出现内存泄漏,暂时我先使用单例的方式暂时解决了内存泄漏的原因,但是我还没有找打根本原因.希望有研究的小伙伴可以相互探 ...