[RN] React Native 自定义导航栏随滚动渐变
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 自定义导航栏随滚动渐变的更多相关文章
- React Native自定义导航栏
之前我们学习了可触摸组件和页面导航的使用的使用: 从零学React Native之09可触摸组件 - 从零学React Native之03页面导航 - 经过之前的学习, 我们可以完成一个自定义导航栏了 ...
- [RN] React Native 自定义 底部 弹出 选择框 实现
React Native 自定义 底部选择框 实现 效果如图所示: 实现方法: 一.组件封装 CustomAlertDialog.js import React, {Component} from ' ...
- React Native自定义导航条
Navigator和NavigatorIOS 在开发中,需要实现多个界面的切换,这时候就需要一个导航控制器来进行各种效果的切换.在React Native中RN为我们提供了两个组件:Navigator ...
- React Native 之导航栏
一定要参考官网: https://reactnavigation.org/docs/en/getting-started.html 代码来自慕课网:https://www.imooc.com/cour ...
- React Native 底部导航栏
首先安装:npm install react-native-tab-navigator 然后再引入文件中 import TabNavigator from 'react-native-tab ...
- iOS个人中心渐变动画、微信对话框、标签选择器、自定义导航栏、短信验证输入框等源码
iOS精选源码 简单的个人中心页面-自定义导航栏并予以渐变动画 程序员取悦女票的正确姿势---Tip1(iOS美容篇) iOS 前台重启应用和清除角标的问题 微信原生提醒对话框3.0 JHLikeBu ...
- iOS:自定义导航栏,随着tableView滚动显示和隐藏
自定义导航栏,随着tableView滚动显示和隐藏 一.介绍 自定义导航栏是APP中很常用的一个功能,通过自定义可以灵活的实现动画隐藏和显示效果.虽然处理系统的导航栏也可以实现,但是这个是有弊端的,因 ...
- Taro多端自定义导航栏Navbar+Tabbar实例
运用Taro实现多端导航栏/tabbar实例 (H5 + 小程序 + React Native) 最近一直在捣鼓taro开发,虽说官网介绍支持编译到多端,但是网上大多数实例都是H5.小程序,很少有支持 ...
- ios7以上自定义导航栏标题的字体大小及颜色的方法
自定义导航栏的字体和颜色,只需要自定义一个lable,然后将lable添加到导航栏的titleview中就可以了 代码如下 UILabel *label = [[UILabel alloc] init ...
随机推荐
- Hash冲突的解决--暴雪的Hash算法
Hash冲突的解决--暴雪的Hash算法https://usench.iteye.com/blog/2199399https://www.bbsmax.com/A/kPzOO7a8zx/
- 安装mysql驱动程序
1. MYSQL驱动:https://dev.mysql.com/downloads/connector/odbc/ 64 位版本:https://cdn.mysql.com//Downloads/ ...
- IEEE Fellow 何海波
IEEE Fellow 何海波 11月21日,全球最大的非营利专业技术学会IEEE(电气与电子工程师协会)公布了2018年度新当选Fellow(会士)名单,我校校友何海波教授入选. 何海波,美国罗德岛 ...
- Qt 的两个许可证区别分析:LGPL 和商业协议
Qt 的两个许可证区别分析:LGPL 和商业协议 Qt 有两个许可证:LGPL 和商业协议.这两个协议在现在的 Qt 版本中的代码是完全一致的(潜在含义是,Qt 的早期版本,商业版的 Qt 通常包含有 ...
- 单片机成长之路(51基础篇)- 024 基于 N76E003 的按键按键状态机
前端时间要用按键,搞了个状态机(如图): main.c #include <N76E003.H> #include <SFR_Macro.h> //N76E003 SFR寄存器 ...
- java报错 pom.xml第一行报"org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project...
https://www.cnblogs.com/appium/p/11168441.html 新建Maven项目时,每个pom文件第一行都报错. 一.问题分析 原因就是你的maven的配置文件不是最新 ...
- H5页面基础元素
H5页面结构元素示例 <!DOCTYPE html> <html lang="zh"> <head> <meta charset=&quo ...
- MySQL中的存储过程、游标和存储函数
MySQL中的存储过程首先来看两个问题: 1.什么是存储过程? 存储过程(Stored Procedure)是在数据库系统中,一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存 ...
- laravel框架之即时更改
表单//@foreach($res as $k=>$v) <tr id="{{$v->id}}" > <td>{{$v->id}}< ...
- vue自带开发环境,生产环境,自己搭建测试环境
git 码云地址: https://gitee.com/qichangshui_admin/vueAddTest 参考地址: https://www.jianshu.com/p/bfcfe5fc253 ...