之前,通过官方文档,我们对Navigator简介。 在React Native开发中,官方推荐使用Navigator作为导航指示器,在早期的版本中ios/android中都使用Navigator作为通用导航栏,不过在在后来的版本中,由于Navigator对ios系统兼容较差,所以使用导航往往使用NavigatorIOS组件。
Navigator设置方法: 初始化路由(initialRoute), 配置场景动画(configureScene), 渲染场景(renderScene)。如常见的:
class SimpleView extends Component {
render() {
return (
<Navigator
style={{flex:1}}
initialRoute={{component: FirstPage}}
configureScene={this.configureScene}
renderScene={this.renderScene}/>
);
}
}
NavigatorIOS包装了UIKit的导航功能,可以使用左划功能来返回到上一界面。
barTintColor : 导航条的背景颜色 initalRoute : 在RN 中导航名为“路由”(学过网络的应该明白这个词的意思), 作用就是指路的,大家可以这么理解,这个属性是一个方法,用来初始化导航的。 itemWrapperStyle : 为每一项定制样式,例如设置每一个页面的背景颜色 navigationBarHidden : 为true , 隐藏导航栏。 shadowHidden : 是否隐藏阴影,true/false。 tintColor : 导航栏上按钮的颜色设置。 titleTextColor : 导航栏上字体的颜色 。 translucent : 导航栏是否是半透明的,true/false。 push(route) : 加载一个新的界面(视图或者路由)并且路由到该界面。 pop() : 返回到上一个页面。 popN(n) : 一次性返回N个界面,当n = 1 时,即相当于pop()方法的效果。 replace(route):替换当前的路由。 replacePrevious(route) : 替换前一个页面的视图并且回退过去。 resetTo(route) : 取代最顶层的路由并且回退过去。 popToTop() : 回到最上层视图。 完整代码实例:
class NavigatorDemo extends Component {
render() {
return (
<NavigatorIOS
style={styles.container}
initialRoute={{
title:'movies',//这是navigationController的title。
component:SearchScreen,//注释:这里是要写的是相当于iOS开发里navigationController的rootViewController页面。
}}
/>
);
}
}
二级页面逻辑
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
class SearchScreen extends Component{
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>131231232
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
top:64,
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
module.exports = SearchScreen;