如何在本机中实现选项卡导航?

时间:2018-01-02 11:31:23

标签: react-native tabnavigator

我在反应原生中创建一个新应用程序。在登录屏幕中,我尝试实施标签导航。我想在Login formregister form之间导航。实际上,我不想导航到其他页面。我&# 39;我试图在这两个forms之间导航(就像在课程应用程序中一样)。那可能吗?我不知道如何实现这一点。

以下是反应中的代码

<Container>
    <View style={styles.container}>
        <View style={styles.content}>
            <InputGroup style={styles.input}>
                <Input
                    label="Email"
                    placeholder="email@gmail.com"   />
                <Image source={require('../Images/envelope.png')} style={{width:30, height:30, marginRight:5}} />
            </InputGroup>
            <InputGroup style={styles.input}>

                <Input
                    label="Password"
                    placeholder="Password"
                    secureTextEntry />
                <Image source={require('../Images/lock.png')} style={{width:30, height:30, marginRight:5}} />

            </InputGroup>
            {this.state.isLoading ? ( <Spinner size="small" color="#000000" />
            ) : (
                <Button style={styles.button} onPress={() => this.onPressLogin()} >
                    <Text style={{paddingLeft:50}}>Login</Text>
                </Button>
            )}

            <Tabs selected={this.state.page} style={{backgroundColor:'white'}}
                  selectedStyle={{color:'red'}} onSelect={el=>this.setState({page:el.props.name})}>

                <Text name="first" selectedIconStyle={{borderTopColor: 'red',borderTopWidth:2}}>Login</Text>
                <Text name="second" selectedIconStyle={{borderTopWidth:2,borderTopColor:'red'}}>Register</Text>
            </Tabs>
        </View>

    </View>
</Container>

以下是主屏幕的截图

tab navigation

2 个答案:

答案 0 :(得分:2)

这是你想要的吗?

enter image description here

如果是这样,实施起来并不是很难。

首先,安装react-navigation模块:

npm install --save react-navigation

其次,你提到你不想制作两个屏幕。但是,在这种情况下,它们实际上仍然是2个独立的屏幕。让我们继续制作两个屏幕:LoginScreenRegisterScreen,如下所示:

// This is LoginScreen
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { Icon } from 'react-native-elements';

class LoginScreen extends Component {
    // This is for the tab bar configuration
    static navigationOptions = {
        title: 'Login',
        header: null,
        tabBarIcon: ({ tintColor }) => {
            return (<Icon name='input' size={30} color={tintColor} />)
        }
    };

    render() {
        return (
            <View style={{ ... }}>
                <Text style={{ ... }}>
                    Login Screen
                </Text>
            </View>
        );
    }
}

export default LoginScreen;

// This is RegisterScreen
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { Icon } from 'react-native-elements';

class RegisterScreen extends Component {
    static navigationOptions = {
        title: 'Register',
        header: null,
        tabBarIcon: ({ tintColor }) => {
            return (<Icon name='person-add' size={30} color={tintColor} />)
        }
    };

    render() {
        return (
            <View style={{ ... }}>
                <Text style={{ ... }}>
                    Register Screen
                </Text>
            </View>
        );
    }
}

export default RegisterScreen;

然后,在您的App.js文件中,执行以下操作:

import React, { Component } from 'react';
import { TabNavigator } from 'react-navigation';

import LoginScreen from 'path/to/LoginScreen';
import RegisterScreen from 'path/to/RegisterScreen';

class App extends Component {
    render() {
        const MainNavigator = TabNavigator({
            login: { screen: LoginScreen },
            register: { screen: RegisterScreen }
        });

        return (
            <MainNavigator />
        );
    }
}

export default App;

答案 1 :(得分:0)

简单的流程代码!

import React from 'react';
import { Text, View } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createBottomTabNavigator } from 'react-navigation-tabs';

class HomeScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>Home!</Text>
      </View>
    );
  }
}

class SettingsScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>Settings!</Text>
      </View>
    );
  }
}

const TabNavigator = createBottomTabNavigator({
  Home: HomeScreen,
  Settings: SettingsScreen,
});

const AppContainer = createAppContainer(TabNavigator);

// without the style you will see a blank screen
export default () => (
  <View style={{ flex: 1 }}>
    <AppContainer />
  </View>
);