【问题标题】:Going from single screen app to tab based app in react-native-navigation在 react-native-navigation 中从单屏应用程序到基于选项卡的应用程序
【发布时间】:2017-07-31 17:01:03
【问题描述】:

我想知道如何从单屏应用转变为基于标签的应用。这里有一个类似的问题没有相关答案。

目前我正在这样做

Navigation.startSingleScreenApp({
    screen: {
      screen: 'Login',
      title: 'Login'
    }
  });

  Navigation.startTabBasedApp({
    tabs: [
      {
        label: 'tab1', 
        screen: 'Login', 
        icon: tab1Icon, 
        selectedIcon: tab1Icon, 
        },
        title: 'tab1', 
        navigatorStyle: {}, 
        navigatorButtons: {} 
      },
      {
        label: 'tab2',
        screen: 'tab2',
        icon: tab2Icon,
        selectedIcon: tab2Icon,
        title: 'tab2'
      },
       {
        label: 'tab3',
        screen: 'tab3',
        icon: tab3Icon,
        selectedIcon: tab3Icon,
        title: 'tab3'
      },
  });

所以现在我只是用登录屏幕覆盖第一个选项卡(我在该特定屏幕上隐藏了选项卡”,当我按下登录按钮时,我只需将堆栈向上移动到 tab1 屏幕,其中选项卡可见。

所以即使我有一个 startSingleScreenApp 应用程序仍然从 tabBasedApp 启动,所以我不确定 startSingleScreenApp 有什么作用。

任何有使用此库经验的人可以告诉我如何继续?

【问题讨论】:

标签: android ios reactjs react-native react-native-navigation


【解决方案1】:

当用户成功登录时,我会创建一个模态视图,然后依次显示 TabNavigator。

让我们从您的应用入口点开始,App.js 文件:

import...

export default class App extends React.Component {
  render() {
    return (
        <LoginScreen/>
    );
  }
}

AppRegistry...

这将在启动应用程序时显示您的LoginScreen,并在LoginScreen.js 中显示您将存储模态的位置

import ...

class LoginScreen extends Component {
  constructor(props) {
    super(props);

    this.state = {
      successfulLoginModalVisible: false
    }
  }

 setSuccessfulLoginModalVisible(visible) {
    this.setState({successfulLoginModalVisible: visible});
  }

...

  render() {
    return(
      <ViewContainer>
        <Modal
          animationType={"slide"}
          visible={this.state.successfulLoginModalVisible}
        >
          <SuccessfulLoginScreen onLogout={() => this.hideSuccessfulLoginModal()}/>
        </Modal>
      </ViewContainer>
    );
  }
}

简单地说,在本机反应中,模态将根据应存储在状态中的布尔值呈现自身(即this.state.successfulLoginModalVisible)。要使这个 Modal 可见,只需调用 setSuccessfulLoginModalVisible(true) 函数(例如按下按钮)

最后我们来看看SuccessfulLoginScreen.js

import ...
export const MyAccountStack = StackNavigator({ //This stack will be used to manage the Tab1 stack

  MyAccount: { screen: MyAccountTab },
  ViewFollowers: { screen: ViewFollowersScreen },
  ViewFollowing: { screen: ViewFollowingScreen },
  EditAccount: { screen: EditAccountScreen },

});

export default tabNavigation = TabNavigator ({

  Tab1: {screen: MyAccountStack,
      navigationOptions: {
        tabBarLabel: 'My Account',
      }
  },
  Tab2: {screen: ...},
  Tab3: {screen: ...},
  }, {
    tabBarPosition: 'bottom',
    swipeEnabled: true,
    tabBarOptions: {
        activeTintColor: 'white',
        activeBackgroundColor: 'darkgrey',
    }
});

然后,此逻辑将允许您为每个单独的选项卡维护一个堆栈。

这是我将采取的方法。请注意,我一直强调我在 iOS 上的开发;我还没有在 Android 上检查过这种行为(但我相信应该不会有太大的差异,如果有的话)。希望我能帮上忙!

【讨论】:

    【解决方案2】:

    在 react-native-navigation 中,假设您可以多次调用 startSingleScreenApp 和 startSingleScreenApp。

    例如,您可以使用它通过登录或主屏幕启动应用程序:

    Navigation.startSingleScreenApp({
        // ...
    })
    

    稍后,您可以通过该屏幕或抽屉中的按钮调用:

    Navigation.startTabBasedApp({
        // ...
    })
    

    并且导航器将启动基于选项卡的界面,替换之前的导航堆栈。

    两者都可以包含相同的抽屉配置。

    您可以稍后再次调用 startSingleScreenApp() 以转到另一个屏幕,依此类推。您只是在使用新的导航堆栈替换导航器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多