标签:native dba png gns asc back 切换 trunc chat
在很久之前,RN中文网说推荐用react-navigation替代navigator作为新的导航库,从RN 0.43版本开始,官方就已经停止维护Navigator了,所以强烈建议大家迁移到新的react-navigation库,而且新的导航库无论从性能还是易用性上都要大大好于老的Navigator!
附上文档地址
接下来我们看react-navigation的使用。
在项目根目录执行:
import Home from ‘./Home‘; import Mains from ‘./Second‘;
const App = StackNavigator({
Home: {
screen: Home,
navigationOptions: {
headerTitle: ‘Home‘, //headerTitle和title效果相同 设置导航栏标题,推荐用这个方法
headerTitleStyle: {color: ‘red‘, backgroundColor: ‘white‘, alignSelf: ‘center‘}, //标题栏文字样式 设置居中
headerStyle: {backgroundColor: ‘green‘}, //导航栏的样式
//header: null //隐藏导航栏
headerRight: <Text onPress={() => {
alert(‘i am right‘);
}
}>right</Text>, //标题栏左右的按钮
headerLeft: <Text style={{marginLeft: 10}}>left</Text>, //最好定义在组件内部
}
},
First: {
screen: First,
navigationOptions: ({navigation}) => ({
headerTitle: ‘i am first‘,
headerStyle: {backgroundColor: ‘green‘}, //导航栏的样式
headerTitleStyle: {color: ‘red‘, alignSelf: ‘center‘, backgroundColor: ‘white‘},
headerLeft: <Button title={‘go back‘} onPress={() => {
navigation.goBack();
}}/>,
})
},
Second: {
screen: Second,
navigationOptions: {}
},
Tab: {
screen: Tab,
navigationOptions: {
title: ‘Tab‘,
}
},
});
路由器的选项:
initialRouteName - 设置堆栈的默认屏幕。必须匹配路由配置中的一个密钥。也就是已有的路由名字initialRouteParams - 初始路线的参数navigationOptions - 用于屏幕的默认导航选项paths - 路由配置中设置的路径的覆盖映射视觉选项:
mode - 定义渲染和转换的样式:
card - 使用标准的iOS和Android屏幕转换。这是默认值。modal - 使屏幕从底部滑入,这是普通的iOS模式。只适用于iOS,对Android无影响。headerMode - 指定标题应如何呈现:
float - 渲染一个保持在顶部的标题,并且随着画面的改变而呈现动画。这是iOS上的常见模式。screen - 每个屏幕都有一个标题,标题与屏幕一起淡入淡出。这是Android上的常见模式。none - 不会显示标题。cardStyle - 使用此支路覆盖或扩展堆叠中单个卡的默认样式。transitionConfig - 返回覆盖默认屏幕转换的对象的功能。onTransitionStart - 卡转换动画即将开始时调用的功能。onTransitionEnd - 卡转换动画完成后要调用的功能。title 串可以用作回退和headerTitletabBarLabel
header React元素或给定的函数返回一个React元素,以显示为标题。设置隐藏标题。HeaderPropsnull
headerTitle 标题使用的字符串或React元素。默认为场景title
headerBackTitle iOS上的后退按钮使用的标题字符串或禁用标签。默认为场景nulltitle
headerTruncatedBackTitle 当返回按钮不适合屏幕时使用的标题字符串。默认。headerBackTitle"Back"
headerRight 反应元素显示在标题的右侧
headerLeft 反应元素显示在标题的左侧
headerStyle 标题的样式对象
headerTitleStyle 标题组件的样式对象
headerBackTitleStyle 样式对象为后面的标题
headerTintColor 标题颜色
headerPressColorAndroid 颜色纹理(Android> = 5.0)
gesturesEnabled #是否可以使用手势关闭此屏幕。在iOS上默认为true,在Android上为false。
static navigationOptions = ({navigation}) => ({
title: ` ${navigation.state.params.user.name}`,
headerRight:<Button title={‘标题‘}/>
})
当然你也可以换成其他组件,也可以添加点击事件等等。也可以一起配。const MyApp = TabNavigator({
Home: {
screen: Home,
navigationOptions: { //也可以写在组件内部的static navigationOptions内
tabBarLabel: ‘首页‘,
tabBarIcon: ({tintColor}) => (<Image source={require(‘./images/camera.png‘)}
style={{tintColor: tintColor}}/>)
}
},
Notifications: {
screen: Two,
navigationOptions: {
tabBarLabel: ‘2页‘,
tabBarIcon: ({tintColor}) => (<Image source={require(‘./images/add.png‘)}
style={[{tintColor: tintColor}, styles.icon]}/>)
}
},
Me: {
screen: Three,
navigationOptions: {
tabBarLabel: ‘3页‘,
tabBarIcon: ({tintColor}) => (<Image source={require(‘./images/more.png‘)}
style={{tintColor: tintColor}}/>)
}
}
},
{
animationEnabled: false, // 切换页面时是否有动画效果
tabBarPosition: ‘bottom‘, // 显示在底端,android 默认是显示在页面顶端的
swipeEnabled: true, // 是否可以左右滑动切换tab
backBehavior: ‘none‘, // 按 back 键是否跳转到第一个Tab(首页), none 为不跳转
tabBarOptions: {
activeTintColor: ‘#ff8500‘, // 文字和图片选中颜色
inactiveTintColor: ‘#999‘, // 文字和图片未选中颜色
showIcon: true, // android 默认不显示 icon, 需要设置为 true 才会显示
indicatorStyle: {
height: 0 // 如TabBar下面显示有一条线,可以设高度为0后隐藏
},
style: {
backgroundColor: ‘#fff‘, // TabBar 背景色
// height: 44
},
labelStyle: {
fontSize: 10, // 文字大小
},
},
});
tabBarComponent- 要用作标签栏的组件,例如 (这是iOS上的默认设置), (这是Android上的默认设置)TabBarBottomTabBarToptabBarPosition- 标签栏的位置可以是或‘top‘‘bottom‘swipeEnabled - 是否允许在标签之间进行滑动animationEnabled - 是否在更改标签时进行动画处理lazy - 是否根据需要懒惰呈现标签,而不是提前制作tabBarOptions - 配置标签栏,如下所示。几个选项被传递到底层路由器来修改导航逻辑:
initialRouteName - 第一次加载时初始标签路由的routeNameorder - 定义选项卡顺序的routeNames数组paths - 将routeName映射到路径配置,该配置将覆盖routeConfigs中设置的路径。backBehavior - 后退按钮是否会使Tab键切换到初始选项卡?如果是的话,设置为,否则。默认为行为。initialRoutenoneinitialRoutetabBarOptionsfor (iOS上的默认标签栏)TabBarBottom activeTintColor - 活动标签的标签和图标颜色activeBackgroundColor - 活动选项卡的背景颜色inactiveTintColor - 不活动标签的标签和图标颜色inactiveBackgroundColor - 非活动标签的背景颜色showLabel - 是否显示标签的标签,默认为truestyle - 标签栏的样式对象labelStyle - 选项卡标签的样式对象tabStyle - 标签的样式对象例:
tabBarOptions: {
activeTintColor: ‘#e91e63‘,
labelStyle: {
fontSize: 12,
},
style: {
backgroundColor: ‘blue‘,
},
}
tabBarOptionsfor (Android上的默认标签栏)TabBarTop activeTintColor - 活动标签的标签和图标颜色inactiveTintColor - 不活动标签的标签和图标颜色showIcon - 是否显示标签的图标,默认值为falseshowLabel - 是否显示标签的标签,默认为trueupperCaseLabel - 是否使标签大写,默认为truepressColor - 颜色纹理(Android> = 5.0)pressOpacity - 按压标签的不透明度(iOS和Android <5.0 only)scrollEnabled - 是否启用可滚动选项卡tabStyle - 标签的样式对象indicatorStyle - 标签指示器的样式对象(选项卡底部的行)labelStyle - 选项卡标签的样式对象iconStyle - 标签图标的样式对象style - 标签栏的样式对象例:
tabBarOptions: {
labelStyle: {
fontSize: 12,
},
tabStyle: {
width: 100,
},
style: {
backgroundColor: ‘blue‘,
},
}
title 通用标题可以用作备用和headerTitletabBarLabel
tabBarVisible True或false显示或隐藏选项卡栏,如果未设置,则默认为true
tabBarIcon React Element或者一个给定的函数返回一个React.Element,以在tab栏中显示{ focused: boolean, tintColor: string }
tabBarLabel 标签栏或React元素中显示的标签的标题字符串或者给定的函数返回一个React.Element,以在标签栏中显示。当未定义时,使用场景。要隐藏,请参见上一节。{ focused: boolean, tintColor: string }titletabBarOptions.showLabel
用于轻松设置带抽屉导航的屏幕。有关实例,请参阅我们的展会演示。
class MyHomeScreen extends React.Component {
static navigationOptions = {
drawerLabel: ‘Home‘,
drawerIcon: ({ tintColor }) => (
<Image
source={require(‘./chats-icon.png‘)}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
};
render() {
return (
<Button
onPress={() => this.props.navigation.navigate(‘Notifications‘)}
title="Go to notifications"
/>
);
}
}
class MyNotificationsScreen extends React.Component {
static navigationOptions = {
drawerLabel: ‘Notifications‘,
drawerIcon: ({ tintColor }) => (
<Image
source={require(‘./notif-icon.png‘)}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
};
render() {
return (
<Button
onPress={() => this.props.navigation.goBack()}
title="Go back home"
/>
);
}
}
const styles = StyleSheet.create({
icon: {
width: 24,
height: 24,
},
});
const MyApp = DrawerNavigator({
Home: {
screen: MyHomeScreen,
},
Notifications: {
screen: MyNotificationsScreen,
},
});
打开和关闭抽屉,导航到并分别。‘DrawerOpen‘‘DrawerClose‘
this.props.navigation.navigate(‘DrawerOpen‘); // open drawer
this.props.navigation.navigate(‘DrawerClose‘); // close drawer
DrawerNavigator(RouteConfigs, DrawerNavigatorConfig)
路由CONFIGS对象是从路由名称映射到一个路由配置,它告诉导航以呈现该路线什么,参见例如由。StackNavigator
drawerWidth - 抽屉的宽度drawerPosition- 选项是或。默认是位置。leftrightleftcontentComponent - 用于呈现抽屉内容的组件,例如导航项。接收抽屉的支柱。默认为。有关详细信息,请参阅下文。navigationDrawerItemscontentOptions - 配置抽屉内容,见下文。默认是不可滚动。要实现可滚动,您必须使用该属性定制容器,如下面的示例所示。DrawerViewViewcontentComponent
{
drawerWidth: 200,
drawerPosition: ‘right‘,
contentComponent: props => <ScrollView><DrawerItems {...props} /></ScrollView>
}
几个选项被传递到底层路由器来修改导航逻辑:
initialRouteName - 初始路由的routeName。order - 定义抽屉物品顺序的routeNames数组。paths - 将routeName映射到路径配置,该配置将覆盖routeConfigs中设置的路径。backBehavior - 后退按钮是否会切换到初始路线?如果是的话,设置为,否则。默认为行为。initialRoutenoneinitialRoutecontentComponent 您可以轻松地覆盖所使用的默认组件:react-navigation
import { DrawerItems } from ‘react-navigation‘;
const CustomDrawerContentComponent = (props) => (
<View style={style.container}>
<DrawerItems {...props} />
</View>
);
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
contentOptions为DrawerItems items - 路由数组,可以修改或覆盖activeItemKey - 标识活动路线的钥匙activeTintColor - 活动标签的标签和图标颜色activeBackgroundColor - 活动标签的背景颜色inactiveTintColor - 无效标签的标签和图标颜色inactiveBackgroundColor - 非活动标签的背景颜色onItemPress(route) - 按下项目时调用的功能style - 内容部分的样式对象labelStyle- 当您的标签是字符串时,样式对象将覆盖内容部分中的样式TextcontentOptions: {
activeTintColor: ‘#e91e63‘,
style: {
marginVertical: 0,
}
}
title 通用标题可以用作备用和headerTitledrawerLabel
drawerLabel String,React元素或给定的函数返回一个React.Element,显示在抽屉侧边栏中。当不确定,现场使用{ focused: boolean, tintColor: string }title
drawerIcon React Element或一个函数,返回一个React.Element,显示在抽屉侧边栏中{ focused: boolean, tintColor: string }
创建的导航器组件需要以下道具:DrawerNavigator(...)
screenProps - 向儿童屏幕传递额外的选项,例如:const DrawerNav = DrawerNavigator({
// config
});
<DrawerNav
screenProps={/* this prop will get passed to the screen components and nav options as props.screenProps */}
/>
DrawerNavigation 请记住,如果您嵌套DrawerNavigation,抽屉将显示在父导航下方。
引用原文:http://blog.csdn.net/u011272795/article/details/72855899
写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!
如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!
标签:native dba png gns asc back 切换 trunc chat
原文地址:http://www.cnblogs.com/summary-2017/p/7571813.html