先放项目地址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的更多相关文章

  1. iOS 开发笔记 -- 各种细枝末节的知识(水滴石穿)

    在此总结整理,遇到的各种的小问题: 1.通过从字典(数组)中取出的NSString的length==0 作为if的判断条件导致的carsh: 由于在字典中通过Key取出值之后直接做了length相关操 ...

  2. 【水滴石穿】react-native-book

    先推荐一个学习的地址:https://ke.qq.com/webcourse/index.html#cid=203313&term_id=100240778&taid=12778558 ...

  3. 【水滴石穿】rnTest

    其实就是一个小的demo,不过代码分的挺精巧的 先放地址:https://github.com/linchengzzz/rnTest 来看看效果 确实没有什么可以说的,不过代码部分还行 先入口文件 / ...

  4. 【水滴石穿】react-native-ble-demo

    项目的话,是想打开蓝牙,然后连接设备 点击已经连接的设备,我们会看到一些设备 不过我这边在开启蓝牙的时候报错了 先放作者的项目地址: https://github.com/hezhii/react-n ...

  5. 【水滴石穿】ReactNative-Redux-Thunk

    老实说,运行出来的项目让人失望,毕竟我想看各种有趣的demo啊- 先放上源码地址:https://github.com/ludejun/ReactNative-Redux-Thunk 我们来一起看看代 ...

  6. 【水滴石穿】mobx-todos

    我觉得代码在有些程序员手里,就好像是画笔,可以创造很多东西 不要觉得创意少就叫没有创意,每天进步一点点,世界更美好 首先源码地址为:https://github.com/byk04712/mobx-t ...

  7. 【水滴石穿】ReactNativeMobxFrame

    项目地址如下:https://github.com/FTD-ZF/ReactNativeMobxFrame 应该可以说的是,项目也只是一个花架子,不过底部的tab稍微改变了 我们一起来看代码 //in ...

  8. 【水滴石穿】react-native-aze

    说个题外话,早上打开电脑的时候,电脑变成彩色的了,锅是我曾经安装的一个chrome扩展,没有经过我的同意开启了 (也许是昨天迷迷糊糊开启了) 上午运行项目都不成功,还以为被黑客攻击了---然后下午就排 ...

  9. 【水滴石穿】douban-movies-react-native

    这个项目的话,倒是可以做一个支架页面,就是你需要什么东西,你就可以在里面加,不过也是比较难的地方 就是数据流,数据处理的部分.react可以处理数据的方式很多,没有见过类似于古老的vue时候可以使用的 ...

随机推荐

  1. 使用ResponseEntity进行返回json数据

    在最近的项目中,与上位机进行数据传输时,上位机需要服务器的响应得知服务器是否正常运行,数据是否正常发送 在最近的调试中我使用ResponseEntity<Map<String,Object ...

  2. zimage 和bzimage 有什么区别

    在网络中,不少服务器采用的是Linux系统.为了进一步提高服务器的性能,可能需要根据特定的硬件及需求重新编译Linux内核.编译Linux 内核,需要根据规定的步骤进行,编译内核过程中涉及到几个重要的 ...

  3. SPOJ - UOFTCG 树的最小路径覆盖

    //SPOJ - UOFTCG 树的最小路径覆盖 #include <bits/stdc++.h> #include <vector> using namespace std; ...

  4. #include <filename.h> 和 #include“filename.h” 有什么区别

    对于#include <filename.h> ,编译器从标准库路径开始搜索filename.h,对于#include “filename.h” ,编译器从用户的工作路径开始搜索filen ...

  5. 【arc072f】AtCoder Regular Contest 072 F - Dam

    题意 有一个体积为L的水池,有N天 每天早上进水Vi体积的Ti温度的水. 每天晚上可以放掉任意体积的水. 问每天中午,水池满的情况下,水温最高多少. 水的温度只受新加进的谁的影响,对于水\(W1(T1 ...

  6. idea创建管理项目

    创建分支时以master为准,这时master上的代码已合并完毕,idea右下角可以看到本地和远程的分支,在本地合并时,先切换到master上,选中要合并到master的分支,选择merge into ...

  7. openCV抠图实验

    #include "pch.h" #include <opencv2/core/core.hpp> #include "opencv2/imgproc/img ...

  8. stream之累加求和

    1.集合中直接包含BigDecimal元素的累加 List<Integer> list = new ArrayList<>();list.add(3);list.add(7); ...

  9. xml转化为数组

    function xml_to_array($xml) { return json_decode(json_encode(simplexml_load_string($xml)), true); }

  10. Mysql 遇到的一些坑

    1.命令行按回车,或是输入\c,quit 都无法结束编辑状态,如图: 出现了"> ,然后不管输入什么都无法退出,这时输入>前的字符作为结束字符,再输入\c,既需要输入 " ...