我正在使用react native router flux
在我的React Native Project中进行导航。Router flux
具有默认的navBar
。
有没有一种方法来自定义navBar
?例如,文本和背景的颜色改变。
我尝试在node_modules/react-native-router-flux/src/navBar.js
中编辑该文件,但似乎不起作用。
请帮助我。
在您的router.js文件中创建场景中的navbar属性,例如:
navBar={NavBar}
,在这里我的纳维尔实际上是一个navbar.js文件,我在其中自定义了导航栏
如果对我有帮助
,请查看我的代码router.js文件:
import React from 'react';
import { Scene, Router } from 'react-native-router-flux';
import mainScreen from './components/mainScreen';
import challengeScreen from './components/challengeScreen';
import NavBar from './components/NavBar';
const RouterComponent = () => {
return (
<Router>
<Scene key="root">
<Scene key="homeScreen" component={mainScreen} hideNavBar={1} />
<Scene
key="screen2" component={challengeScreen} navTransparent={1}
navBar={NavBar}
/>
</Scene>
</Router>
);
};
export default RouterComponent;
navbar.js文件
import {
View, Image, StatusBar, TouchableWithoutFeedback
} from 'react-native';
import React, { Component } from 'react';
import { Actions, Router, Scene } from 'react-native-router-flux';
class NavBar extends Component {
render() {
return (
<View style={styles.backgroundStyle}>
<StatusBar />
<View style={{ flexDirection: 'row' }}>
<TouchableWithoutFeedback onPress={() => Actions.homeScreen()}>
<Image
source={require('./Images/back-arrow.png')}
style={styles.backarrowStyle} />
</TouchableWithoutFeedback>
<Image
source={require('./Images/help.png')}
style={styles.helpStyle} />
<Image
source={require('./Images/setting.png')}
style={styles.settingStyle} />
</View>
</View>
);
}
}
const styles = {
backgroundStyle: {
backgroundColor: 'transparent'
},
backarrowStyle: {
resizeMode: 'contain',
flexDirection: 'row',
width: 50,
height: 50,
left: 0,
justifyContent: 'flex-start'
},
helpStyle: {
resizeMode: 'contain',
width: 50,
height: 50,
left: 220,
justifyContent: 'flex-end',
position: 'relative'
},
settingStyle: {
resizeMode: 'contain',
width: 50,
height: 50,
justifyContent: 'flex-end',
position: 'relative',
left: 210
}
};
export default NavBar;
这有助于我自定义导航栏希望它能帮助您
您应该添加navigationBarStyle
属性以自定义导航栏。您可以查看以下代码:
<Scene key="key1" icon={TabIcon} title="book-open">
<Scene key="key2" hideNavBar={false}
navigationBarStyle={{backgroundColor:'transparent',marginTop:8, borderBottomWidth:0}}
component={TestComponent}
title=""/>
</Scene>
此外,此主题也会在此处提及。
示例中有一个有效的示例 react-native-router-flux repository的文件夹。
例如,您的自定义Navbar是:
import { Image, Platform, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import React from 'react';
import { Actions } from 'react-native-router-flux';
const styles = StyleSheet.create({
container: {
height: Platform.OS === 'ios' ? 64 : 54,
flexDirection: 'row',
paddingTop: 20,
},
navBarItem: {
flex: 1,
justifyContent: 'center',
},
});
export default class CustomNavBar extends React.Component {
// constructor(props) {
// super(props)
// }
_renderLeft() {
if (Actions.currentScene === 'customNavBar1') {
return (
<TouchableOpacity onPress={() => console.log('Hamburger button pressed')} style={[styles.navBarItem, { paddingLeft: 10 }]}>
<Image
style={{ width: 30, height: 50 }}
resizeMode="contain"
source={{ uri: 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Hamburger_icon.svg/1200px-Hamburger_icon.svg.png' }}
/>
</TouchableOpacity>
);
}
return (
<TouchableOpacity onPress={Actions.pop} style={[styles.navBarItem, { paddingLeft: 10 }]}>
<Image style={{ width: 30, height: 50 }} resizeMode="contain" source={{ uri: 'https://image.flaticon.com/icons/png/512/0/340.png' }} />
</TouchableOpacity>
);
}
_renderMiddle() {
return (
<View style={styles.navBarItem}>
<Text>{this.props.title}</Text>
</View>
);
}
_renderRight() {
return (
<View style={[styles.navBarItem, { flexDirection: 'row', justifyContent: 'flex-end' }]}>
<TouchableOpacity onPress={() => console.log('Share')} style={{ paddingRight: 10 }}>
<Image style={{ width: 30, height: 50 }} resizeMode="contain" source={{ uri: 'https://cdn3.iconfinder.com/data/icons/glypho-free/64/share-512.png' }} />
</TouchableOpacity>
<TouchableOpacity onPress={() => console.log('Search')} style={{ paddingRight: 10 }}>
<Image style={{ width: 30, height: 50 }} resizeMode="contain" source={{ uri: 'https://maxcdn.icons8.com/Share/icon/p1em/Very_Basic//search1600.png' }} />
</TouchableOpacity>
</View>
);
}
render() {
let dinamicStyle = {};
if (Actions.currentScene === 'customNavBar1') {
dinamicStyle = { backgroundColor: 'red' };
} else {
dinamicStyle = { backgroundColor: 'yellow' };
}
return (
<View style={[styles.container, dinamicStyle]}>
{this._renderLeft()}
{this._renderMiddle()}
{this._renderRight()}
</View>
);
}
}
它对我很好。只是不要忘记设置 navbar scene 或 stack> stack :
的属性<Scene navBar={CustomNavBar} key='myKey' component={MyComponent} />
react-native-router-flux提供了一些API,请查看https://github.com/aksonov/react-native-native-native-router-flux/blob/master/docs/api_configuration.md,也许是您需要的。