有没有办法在使用 createBottomTabNavigator 和 createStackNavigator 并同时使用 navigationOptions 时隐藏选项卡项?
Is there a way to hide a tab item when using createBottomTabNavigator and createStackNavigator and at the same time navigationOptions working?
我有三个屏幕,我只想在前两个屏幕上使用标签栏。在这两个屏幕中,我放置了一个导航到第三个屏幕的按钮。
我的第一个方法是代码:
import React from "react";
import { Platform } from "react-native";
import {
createStackNavigator,
createBottomTabNavigator,
StackViewTransitionConfigs
} from "react-navigation";
import TabBarIcon from "../components/TabBarIcon";
import HomeScreen from "../screens/HomeScreen";
import LinksScreen from "../screens/LinksScreen";
import SettingsScreen from "../screens/SettingsScreen";
const Tabs = createBottomTabNavigator({
Home: {
screen: HomeScreen,
navigationOptions: ({ navigation }) => ({
tabBarLabel: "Home",
title: "Tahiry",
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={
Platform.OS === "ios"
? `ios-information-circle${focused ? "" : "-outline"}`
: "md-information-circle"
}
/>
)
})
},
Links: {
screen: LinksScreen,
navigationOptions: ({ navigation }) => ({
tabBarLabel: "Links",
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={Platform.OS === "ios" ? "ios-link" : "md-link"}
/>
)
})
}
});
export default createStackNavigator({
tabs: Tabs,
Settings: SettingsScreen
});
它在工作,但 "navigationOptions" 不工作,所以如果我为 header 设置标题(即使我设置 "header:null" 也会始终显示),它不会出现。
我尝试了下一个代码的另一种方法,但我无法隐藏第三个选项卡项:
import React from 'react';
import { Platform } from 'react-native';
import { createStackNavigator, createBottomTabNavigator } from 'react-navigation';
import TabBarIcon from '../components/TabBarIcon';
import HomeScreen from '../screens/HomeScreen';
import LinksScreen from '../screens/LinksScreen';
import SettingsScreen from '../screens/SettingsScreen';
const HomeStack = createStackNavigator({
Home: HomeScreen,
});
HomeStack.navigationOptions = {
tabBarLabel: 'Home',
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={
Platform.OS === 'ios'
? `ios-information-circle${focused ? '' : '-outline'}`
: 'md-information-circle'
}
/>
),
};
const LinksStack = createStackNavigator({
Links: LinksScreen,
});
LinksStack.navigationOptions = {
tabBarLabel: 'Links',
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={Platform.OS === 'ios' ? 'ios-link' : 'md-link'}
/>
),
};
const SettingsStack = createStackNavigator({
Settings: SettingsScreen,
});
SettingsStack.navigationOptions = {
tabBarLabel: 'Settings',
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={Platform.OS === 'ios' ? 'ios-options' : 'md-options'}
/>
),
};
export default createBottomTabNavigator({
HomeStack,
LinksStack,
SettingsStack,
});
欢迎任何建议。
使用 defaultNavigationOptions 自定义您的 tabBar
defaultNavigationOptions: ({ navigation }) => ({
// You can return any component that you like here!
return <View>
<View>
<Text> Tab 1</Text>
</View>
<View>
<Text> Tab =2</Text>
</View>
</View>
},
}),
如果您想隐藏 StackNavigator 中的选项卡,可以试试这个
const HomeTab = createStackNavigator({
Home:{screen: HomeScreen,},
Settings:{screen: SettingsScreen,}
}, {initialRouteName: 'Home', headerMode: 'none')}
HomeTab.navigationOptions = ({navigation}) => {
let tabBarVisible = true;
if(navigation.state.index > 0){
tabBarVisible = false;
}
return {
tabBarVisible,
}
}
const TabNavi = createBottomTabNavigator({
Home:{screen: HomeTab,},
Links :{screen: LinksScreen,},
})
export default TabNavi;
我终于设法让它工作了。解决方案来自第二种方法。
首先,您必须像这样在堆栈中声明您的场景:
const HomeStack = createStackNavigator({
Home: HomeScreen,
Details1: DetailsScreen
});
然后你必须单独导入tabBarIcon,我认为这是必要的,因为createStackNavigator没有TabBarIcon方法
import TabBarIcon from "../components/TabBarIcon";
您现在可以使用导航和图标,您可以在堆栈中添加任意数量的屏幕。并且您只为堆栈创建选项卡。
完整示例如下:
import React from "react";
import {
Platform,
StatusBar,
StyleSheet,
View,
Button,
Text
} from "react-native";
import {
createBottomTabNavigator,
createStackNavigator,
createAppContainer
} from "react-navigation";
import TabBarIcon from "../components/TabBarIcon";
class DetailsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Details1!</Text>
</View>
);
}
}
class DetailsScreen2 extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Details2!</Text>
</View>
);
}
}
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Home!</Text>
<Button
title="Go to Details1"
onPress={() => this.props.navigation.navigate("Details1")}
/>
</View>
);
}
}
class SettingsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Settings!</Text>
<Button
title="Go to Details2"
onPress={() => this.props.navigation.navigate("Details2")}
/>
</View>
);
}
}
const HomeStack = createStackNavigator({
Home: HomeScreen,
Details1: DetailsScreen
});
HomeStack.navigationOptions = {
tabBarLabel: "Home",
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={
Platform.OS === "ios"
? `ios-information-circle${focused ? "" : "-outline"}`
: "md-information-circle"
}
/>
)
};
const SettingsStack = createStackNavigator({
Settings: SettingsScreen,
Details2: DetailsScreen2
});
SettingsStack.navigationOptions = {
tabBarLabel: "Maison",
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={
Platform.OS === "ios"
? `ios-information-circle${focused ? "" : "-outline"}`
: "md-information-circle"
}
/>
)
};
export default createAppContainer(
createBottomTabNavigator(
{
Home: HomeStack,
Settings: SettingsStack
},
{
/* Other configuration remains unchanged */
}
)
);
这是tabBaricon.js
的内容
import React from 'react';
import { Icon } from 'expo';
import Colors from '../constants/Colors';
export default class TabBarIcon extends React.Component {
render() {
return (
<Icon.Ionicons
name={this.props.name}
size={26}
style={{ marginBottom: -3 }}
color={this.props.focused ? Colors.tabIconSelected : Colors.tabIconDefault}
/>
);
}
}
我有三个屏幕,我只想在前两个屏幕上使用标签栏。在这两个屏幕中,我放置了一个导航到第三个屏幕的按钮。 我的第一个方法是代码:
import React from "react";
import { Platform } from "react-native";
import {
createStackNavigator,
createBottomTabNavigator,
StackViewTransitionConfigs
} from "react-navigation";
import TabBarIcon from "../components/TabBarIcon";
import HomeScreen from "../screens/HomeScreen";
import LinksScreen from "../screens/LinksScreen";
import SettingsScreen from "../screens/SettingsScreen";
const Tabs = createBottomTabNavigator({
Home: {
screen: HomeScreen,
navigationOptions: ({ navigation }) => ({
tabBarLabel: "Home",
title: "Tahiry",
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={
Platform.OS === "ios"
? `ios-information-circle${focused ? "" : "-outline"}`
: "md-information-circle"
}
/>
)
})
},
Links: {
screen: LinksScreen,
navigationOptions: ({ navigation }) => ({
tabBarLabel: "Links",
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={Platform.OS === "ios" ? "ios-link" : "md-link"}
/>
)
})
}
});
export default createStackNavigator({
tabs: Tabs,
Settings: SettingsScreen
});
它在工作,但 "navigationOptions" 不工作,所以如果我为 header 设置标题(即使我设置 "header:null" 也会始终显示),它不会出现。 我尝试了下一个代码的另一种方法,但我无法隐藏第三个选项卡项:
import React from 'react';
import { Platform } from 'react-native';
import { createStackNavigator, createBottomTabNavigator } from 'react-navigation';
import TabBarIcon from '../components/TabBarIcon';
import HomeScreen from '../screens/HomeScreen';
import LinksScreen from '../screens/LinksScreen';
import SettingsScreen from '../screens/SettingsScreen';
const HomeStack = createStackNavigator({
Home: HomeScreen,
});
HomeStack.navigationOptions = {
tabBarLabel: 'Home',
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={
Platform.OS === 'ios'
? `ios-information-circle${focused ? '' : '-outline'}`
: 'md-information-circle'
}
/>
),
};
const LinksStack = createStackNavigator({
Links: LinksScreen,
});
LinksStack.navigationOptions = {
tabBarLabel: 'Links',
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={Platform.OS === 'ios' ? 'ios-link' : 'md-link'}
/>
),
};
const SettingsStack = createStackNavigator({
Settings: SettingsScreen,
});
SettingsStack.navigationOptions = {
tabBarLabel: 'Settings',
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={Platform.OS === 'ios' ? 'ios-options' : 'md-options'}
/>
),
};
export default createBottomTabNavigator({
HomeStack,
LinksStack,
SettingsStack,
});
欢迎任何建议。
使用 defaultNavigationOptions 自定义您的 tabBar
defaultNavigationOptions: ({ navigation }) => ({
// You can return any component that you like here!
return <View>
<View>
<Text> Tab 1</Text>
</View>
<View>
<Text> Tab =2</Text>
</View>
</View>
},
}),
如果您想隐藏 StackNavigator 中的选项卡,可以试试这个
const HomeTab = createStackNavigator({
Home:{screen: HomeScreen,},
Settings:{screen: SettingsScreen,}
}, {initialRouteName: 'Home', headerMode: 'none')}
HomeTab.navigationOptions = ({navigation}) => {
let tabBarVisible = true;
if(navigation.state.index > 0){
tabBarVisible = false;
}
return {
tabBarVisible,
}
}
const TabNavi = createBottomTabNavigator({
Home:{screen: HomeTab,},
Links :{screen: LinksScreen,},
})
export default TabNavi;
我终于设法让它工作了。解决方案来自第二种方法。 首先,您必须像这样在堆栈中声明您的场景:
const HomeStack = createStackNavigator({
Home: HomeScreen,
Details1: DetailsScreen
});
然后你必须单独导入tabBarIcon,我认为这是必要的,因为createStackNavigator没有TabBarIcon方法
import TabBarIcon from "../components/TabBarIcon";
您现在可以使用导航和图标,您可以在堆栈中添加任意数量的屏幕。并且您只为堆栈创建选项卡。
完整示例如下:
import React from "react";
import {
Platform,
StatusBar,
StyleSheet,
View,
Button,
Text
} from "react-native";
import {
createBottomTabNavigator,
createStackNavigator,
createAppContainer
} from "react-navigation";
import TabBarIcon from "../components/TabBarIcon";
class DetailsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Details1!</Text>
</View>
);
}
}
class DetailsScreen2 extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Details2!</Text>
</View>
);
}
}
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Home!</Text>
<Button
title="Go to Details1"
onPress={() => this.props.navigation.navigate("Details1")}
/>
</View>
);
}
}
class SettingsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Settings!</Text>
<Button
title="Go to Details2"
onPress={() => this.props.navigation.navigate("Details2")}
/>
</View>
);
}
}
const HomeStack = createStackNavigator({
Home: HomeScreen,
Details1: DetailsScreen
});
HomeStack.navigationOptions = {
tabBarLabel: "Home",
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={
Platform.OS === "ios"
? `ios-information-circle${focused ? "" : "-outline"}`
: "md-information-circle"
}
/>
)
};
const SettingsStack = createStackNavigator({
Settings: SettingsScreen,
Details2: DetailsScreen2
});
SettingsStack.navigationOptions = {
tabBarLabel: "Maison",
tabBarIcon: ({ focused }) => (
<TabBarIcon
focused={focused}
name={
Platform.OS === "ios"
? `ios-information-circle${focused ? "" : "-outline"}`
: "md-information-circle"
}
/>
)
};
export default createAppContainer(
createBottomTabNavigator(
{
Home: HomeStack,
Settings: SettingsStack
},
{
/* Other configuration remains unchanged */
}
)
);
这是tabBaricon.js
的内容import React from 'react';
import { Icon } from 'expo';
import Colors from '../constants/Colors';
export default class TabBarIcon extends React.Component {
render() {
return (
<Icon.Ionicons
name={this.props.name}
size={26}
style={{ marginBottom: -3 }}
color={this.props.focused ? Colors.tabIconSelected : Colors.tabIconDefault}
/>
);
}
}