【水滴石穿】rn_statusbar
先放项目地址https://github.com/hezhii/rn_statusbar
来看一下效果




咩有感觉很怎么样,看代码
根入口文件
//index.js
//看代码我们知道入口是app.js
import { AppRegistry } from 'react-native';
import App from './src/App';
import { name as appName } from './app.json';
AppRegistry.registerComponent(appName, () => App);
app.js
//src/App.js
//里面主要是引用了navigator.js
import React, { PureComponent } from 'react';
import Navigator from './Navigator'
export default class App extends PureComponent {
render() {
return <Navigator />
}
}
//src/Navigator.js
import React from 'react'
import { createAppContainer, createStackNavigator, createBottomTabNavigator } from 'react-navigation'
import TabBarIcon from './components/TabBarIcon'
import Home from './pages/Home'
import My from './pages/My'
import Login from './pages/Login'
import Register from './pages/Register'
const Main = createBottomTabNavigator(
{
Home,
My
},
{
defaultNavigationOptions: ({ navigation }) => {
const { routeName } = navigation.state;
return {
tabBarIcon: ({ focused }) => <TabBarIcon focused={focused} routeName={routeName} />,
};
},
tabBarOptions: {
activeTintColor: '#437dff',
inactiveTintColor: '#888FA1',
style: {
borderTopColor: '#E6E8EB',
},
},
}
)
export default createAppContainer(createStackNavigator(
{
Main: {
screen: Main,
navigationOptions: {
header: null,
},
},
Login,
Register
},
{
defaultNavigationOptions: {
headerBackTitle: '返回'
}
}
))

//src/pages/Home.js
import React from 'react'
import { StyleSheet, View, ImageBackground, Button, StatusBar } from 'react-native'
import Header from '../components/Header'
import { setStatusBar } from '../components/HOC/StatusBar'
@setStatusBar({
barStyle: 'light-content',
translucent: true,
backgroundColor: 'transparent'
})
export default class Home extends React.PureComponent {
static navigationOptions = {
title: '主页'
}
render() {
return (
<View style={styles.fill}>
<ImageBackground style={styles.bg} source={require('../assets/imgs/bg.png')}>
<Header title="主页" fullScreen />
</ImageBackground>
<View style={styles.buttonWrapper}>
<Button
title="退出登录"
onPress={() => this.props.navigation.push('Login')}
color="#437dff"
/>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
fill: {
flex: 1
},
bg: {
height: 234,
},
text: {
fontSize: 20,
fontWeight: '500',
color: '#437dff',
textAlign: 'center'
},
buttonWrapper: {
padding: 16
}
})
//src/pages/Login.js
import React from 'react'
import { StyleSheet, View, Button } from 'react-native'
import { setStatusBar } from '../components/HOC/StatusBar'
@setStatusBar()
export default class Login extends React.PureComponent {
static navigationOptions = {
title: '登录',
}
render() {
return (
<View style={styles.fill}>
<View style={styles.buttonWrapper}>
<Button
title="点击注册"
onPress={() => this.props.navigation.push('Register')}
color="#437dff"
/>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
fill: {
flex: 1
},
buttonWrapper: {
padding: 16
}
})

//src/pages/My.js
import React from 'react'
import { StyleSheet, View, Button } from 'react-native'
import Header from '../components/Header'
import { setStatusBar } from '../components/HOC/StatusBar'
@setStatusBar({
barStyle: 'light-content',
translucent: true,
backgroundColor: 'transparent'
})
export default class My extends React.PureComponent {
static navigationOptions = {
title: '我的',
}
render() {
return (
<View style={styles.fill}>
<Header title="我的" style={styles.header} fullScreen />
<View style={styles.buttonWrapper}>
<Button
title="退出登录"
onPress={() => this.props.navigation.push('Login')}
color="#437dff"
/>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
fill: {
flex: 1
},
text: {
fontSize: 20,
fontWeight: '500',
color: '#437dff',
textAlign: 'center'
},
header: {
backgroundColor: '#437dff',
},
buttonWrapper: {
padding: 16
}
})
//src/pages/Register.js
import React from 'react'
import { StyleSheet, View, Text, StatusBar } from 'react-native'
export default class Login extends React.PureComponent {
static navigationOptions = {
title: '注册',
headerStyle: {
backgroundColor: '#437dff',
},
headerTintColor: '#fff'
}
render() {
return (
<View style={styles.fill}>
<StatusBar translucent={false} backgroundColor='red' barStyle="light-content" />
<Text style={styles.text}>注册页面</Text>
</View>
)
}
}
const styles = StyleSheet.create({
fill: {
flex: 1
},
text: {
marginTop: 32,
fontSize: 20,
fontWeight: '500',
color: '#437dff',
textAlign: 'center'
}
})
工具函数里面是做的适配
//src/utils/device.js
import { Platform, Dimensions } from 'react-native';
// iPhone X、iPhone XS
const X_WIDTH = 375;
const X_HEIGHT = 812;
// iPhone XR、iPhone XS Max
const XSMAX_WIDTH = 414;
const XSMAX_HEIGHT = 896;
const DEVICE_SIZE = Dimensions.get('window');
const { height: D_HEIGHT, width: D_WIDTH } = DEVICE_SIZE;
export { DEVICE_SIZE };
export const isiOS = () => Platform.OS === 'ios'
export const isAndroid = () => Platform.OS === 'android'
export const isiPhoneX = () => {
return (
isiOS() &&
((D_HEIGHT === X_HEIGHT && D_WIDTH === X_WIDTH) ||
(D_HEIGHT === X_WIDTH && D_WIDTH === X_HEIGHT)) ||
((D_HEIGHT === XSMAX_HEIGHT && D_WIDTH === XSMAX_WIDTH) ||
(D_HEIGHT === XSMAX_WIDTH && D_WIDTH === XSMAX_HEIGHT))
);
};
export const ifiPhoneX = (iPhoneXStyle, regularStyle) => isiPhoneX() ? iPhoneXStyle : regularStyle
封装的header
//src/components/Header/index.js
/**
* 全屏页面中使用的 Header 组件。非全屏页面使用 react-nativetion 的 Header 即可。
*
* 组件会根据当前运行的环境调整高度,考虑了状态栏。
*/
import React from 'react';
import { StyleSheet, View, Text, StatusBar } from 'react-native';
import { isiOS, isiPhoneX } from '../../utils/device'
const STATUS_BAR_HEIGHT = isiOS() ? (isiPhoneX() ? 34 : 20) : StatusBar.currentHeight
const HEADER_HEIGHT = 44
const Header = ({ title, left, right, color = '#fff', style, fullScreen }) => {
const headerStyle = [
styles.header,
(fullScreen || isiOS()) && {
height: STATUS_BAR_HEIGHT + HEADER_HEIGHT,
paddingTop: STATUS_BAR_HEIGHT
},
style
]
return (
<View style={headerStyle}>
<View style={styles.left}>
{left}
</View>
<Text style={[styles.title, { color }]}>{title}</Text>
<View style={styles.right}>
{right}
</View>
</View>
);
};
const styles = StyleSheet.create({
header: {
height: HEADER_HEIGHT,
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 15,
},
title: {
flex: 2,
fontSize: 17,
textAlign: 'center',
},
left: {
flex: 1,
flexDirection: 'row',
},
right: {
flex: 1,
flexDirection: 'row',
justifyContent: 'flex-end',
},
});
export default Header;
//src/components/TabBarIcon/index.js
import React from 'react';
import { StyleSheet, Image } from 'react-native';
const styles = StyleSheet.create({
image: {
height: 24,
},
});
export default ({ routeName, focused }) => {
const images = {
Home: focused
? require('../../assets/icons/home_fill.png')
: require('../../assets/icons/home.png'),
My: focused
? require('../../assets/icons/my_fill.png')
: require('../../assets/icons/my.png'),
};
return (
<Image style={styles.image} source={images[routeName]} resizeMode="contain" />
);
}
//src/components/HOC/StatusBar.js
//这个是定义的高阶函数
import React from 'react'
import hoistNonReactStatics from 'hoist-non-react-statics'
import { StatusBar } from 'react-native'
import { isAndroid } from '../../utils/device'
export const setStatusBar = (statusbarProps = {}) => WrappedComponent => {
class Component extends React.PureComponent {
constructor(props) {
super(props)
this._navListener = props.navigation.addListener('willFocus', this._setStatusBar)
}
componentWillUnmount() {
this._navListener.remove();
}
_setStatusBar = () => {
const {
barStyle = "dark-content",
backgroundColor = '#fff',
translucent = false
} = statusbarProps
StatusBar.setBarStyle(barStyle)
if (isAndroid()) {
StatusBar.setTranslucent(translucent)
StatusBar.setBackgroundColor(backgroundColor);
}
}
render() {
return <WrappedComponent {...this.props} />
}
}
return hoistNonReactStatics(Component, WrappedComponent);
}
just soso
【水滴石穿】rn_statusbar的更多相关文章
- iOS 开发笔记 -- 各种细枝末节的知识(水滴石穿)
在此总结整理,遇到的各种的小问题: 1.通过从字典(数组)中取出的NSString的length==0 作为if的判断条件导致的carsh: 由于在字典中通过Key取出值之后直接做了length相关操 ...
- 【水滴石穿】react-native-book
先推荐一个学习的地址:https://ke.qq.com/webcourse/index.html#cid=203313&term_id=100240778&taid=12778558 ...
- 【水滴石穿】rnTest
其实就是一个小的demo,不过代码分的挺精巧的 先放地址:https://github.com/linchengzzz/rnTest 来看看效果 确实没有什么可以说的,不过代码部分还行 先入口文件 / ...
- 【水滴石穿】react-native-ble-demo
项目的话,是想打开蓝牙,然后连接设备 点击已经连接的设备,我们会看到一些设备 不过我这边在开启蓝牙的时候报错了 先放作者的项目地址: https://github.com/hezhii/react-n ...
- 【水滴石穿】ReactNative-Redux-Thunk
老实说,运行出来的项目让人失望,毕竟我想看各种有趣的demo啊- 先放上源码地址:https://github.com/ludejun/ReactNative-Redux-Thunk 我们来一起看看代 ...
- 【水滴石穿】mobx-todos
我觉得代码在有些程序员手里,就好像是画笔,可以创造很多东西 不要觉得创意少就叫没有创意,每天进步一点点,世界更美好 首先源码地址为:https://github.com/byk04712/mobx-t ...
- 【水滴石穿】ReactNativeMobxFrame
项目地址如下:https://github.com/FTD-ZF/ReactNativeMobxFrame 应该可以说的是,项目也只是一个花架子,不过底部的tab稍微改变了 我们一起来看代码 //in ...
- 【水滴石穿】react-native-aze
说个题外话,早上打开电脑的时候,电脑变成彩色的了,锅是我曾经安装的一个chrome扩展,没有经过我的同意开启了 (也许是昨天迷迷糊糊开启了) 上午运行项目都不成功,还以为被黑客攻击了---然后下午就排 ...
- 【水滴石穿】douban-movies-react-native
这个项目的话,倒是可以做一个支架页面,就是你需要什么东西,你就可以在里面加,不过也是比较难的地方 就是数据流,数据处理的部分.react可以处理数据的方式很多,没有见过类似于古老的vue时候可以使用的 ...
随机推荐
- Leetcode513. Find Bottom Left Tree Value找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输出: 7 注意: 您可以假 ...
- Tomcat--远程Debug以及参数配置调优
本文会讲解Tomcat远程Debug调试,Tomcat-manager监控(简单带过),psi-probe监控和Tomcat参数调优.本文基于Tomcat8.5版本. Tomcat远程Debug: 远 ...
- 阿里云安全研究成果入选人工智能顶级会议 IJCAI 2019, 业界首次用AI解决又一难题!
8月10日至8月16日,国际人工智能组织联合会议IJCAI 2019(International Joint Conference on Artificial Intelligence 2019)在中 ...
- css的层叠性+继承性+优先级+权重
一.层叠性 1.含义 多种css样式叠加,浏览器处理冲突的能力. 2.原则 1>一般情况下,若出现冲突,会按照css的书写顺序,以最后的样式为准 2>样式不冲突,就不会层叠 二.css的继 ...
- Spring注解驱动开发(四)-----aop、声明式事务
AOP 概念 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式:-----基于动态代理 一个aop示例 1.导入aop模块:Spring AOP:(spring-aspects ...
- 什么情况下要加上【javascript:】
你知道http:// https:// mailto: tencent://这种东西么?这叫url schema,通常是在a的href里的.但a的href里面是不能加脚本的,所以浏览器就创造了一个叫j ...
- 加载框(loading)
一般在用户提交数据或者新加载页面时,请求服务器的过程,页面没有响应,但是用户并不知道,此时在发生什么.这时,就需要loading框给用户提示,增加用户体验. 1.引入loading.css. html ...
- PAT甲级——A1050 String Subtraction
Given two strings S1 and S2, S=S1−S2 is defined to be the remaining string after taking ...
- MyEclipse6.5安装SVN插件方法
MyEclipse6.5安装SVN插件,掌握了几种方法,本节就像大家介绍一下MyEclipse6.5安装SVN插件的三种方法,看完本文你肯定有不少收获,希望本文能教会你更多东西. 一.安装方法: My ...
- Jquery点击加载更多
一.点击加载更多有点像分页获取数据类似,下面是本人写的一个简单的小例子 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona ...