【问题标题】:How to show footer in full screen如何全屏显示页脚
【发布时间】:2019-12-06 09:59:01
【问题描述】:

我有一个页脚,其中包含几个我想全屏显示的按钮。我正在使用以下代码:

    **//App.js**
    export default class App extends React.Component {
      constructor(props) {
        super(props);
      }
      render() {
        return (
          <View style={{flex: 1, backgroundColor: '#6ED4C8'}}>
            <AppNavigator />
            <View style={{flexDirection: 'row'}}>
              <Footer />
            </View>
          </View>
        );
      }
    }



    **//AppNavigator.js**
    const AppNavigator = createStackNavigator(
      {
        Home: {screen: Home},
        Friends: {screen: Friends},
        Login: {screen: Login},
        PostDatiles: {screen: PostDatiles},
        Aboutus: {screen: Aboutus},
        Footer: {screen: Footer},
      },
      {
        headerMode: 'none',
        navigationOptions: {
          headerVisible: false,
        },
        //  ,initialRouteName: ''
      },
    );
    export default AppNavigator;


    **//Footer.js**
     export default class Footer extends React.Component {
      constructor(props) {
        super(props);
      }

      render() {
        const {navigation} = this.props;
        return (
          <View style={styles.container}>
            <View>
              <Button
                title="About us"
                color="#39F80F"
                accessibilityLabel="Tap on Me"
                onPress={() => navigation.navigate('Login')}
              />
            </View>
            <View>
              <Button
                title="Login"
                color="#F8380F"
                accessibilityLabel="Tap on Me"
              />
            </View>
            <View>
              <Button
                title="Search"
                color="#B80FF8"
                accessibilityLabel="Tap on Me"
              />
            </View>
          </View>
        );
      }
    }

    const styles = StyleSheet.create({
      container: {
        flexDirection: 'row',
      },
    });

当我单击其中一个按钮时,我收到此错误: enter image description here

在 app.js 文件中,我使用页脚标签,因为我想在整个屏幕上显示它。

我搜索了很多,但找不到任何类似的问题!

【问题讨论】:

    标签: react-native navigation


    【解决方案1】:

    要做你想做的事,你必须使用createBottomTabNavigator而不是StackNavigator

    示例

    import React from 'react';
    import { Text, View } from 'react-native';
    import { Ionicons } from '@expo/vector-icons'; // 6.2.2
    import { createBottomTabNavigator, createAppContainer } from 'react-navigation';
    
    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>
        );
      }
    }
    
    class IconWithBadge extends React.Component {
      render() {
        const { name, badgeCount, color, size } = this.props;
        return (
          <View style={{ width: 24, height: 24, margin: 5 }}>
            <Ionicons name={name} size={size} color={color} />
            {badgeCount > 0 && (
              <View
                style={{
                  // /If you're using react-native < 0.57 overflow outside of the parent
                  // will not work on Android, see https://git.io/fhLJ8
                  position: 'absolute',
                  right: -6,
                  top: -3,
                  backgroundColor: 'red',
                  borderRadius: 6,
                  width: 12,
                  height: 12,
                  justifyContent: 'center',
                  alignItems: 'center',
                }}>
                <Text style={{ color: 'white', fontSize: 10, fontWeight: 'bold' }}>
                  {badgeCount}
                </Text>
              </View>
            )}
          </View>
        );
      }
    }
    
    const HomeIconWithBadge = props => {
      // You should pass down the badgeCount in some other ways like context, redux, mobx or event emitters.
      return <IconWithBadge {...props} badgeCount={3} />;
    };
    
    const getTabBarIcon = (navigation, focused, tintColor) => {
      const { routeName } = navigation.state;
      let IconComponent = Ionicons;
      let iconName;
      if (routeName === 'Home') {
        iconName = `ios-information-circle${focused ? '' : '-outline'}`;
        // We want to add badges to home tab icon
        IconComponent = HomeIconWithBadge;
      } else if (routeName === 'Settings') {
        iconName = `ios-options${focused ? '' : '-outline'}`;
      }
    
      // You can return any component that you like here!
      return <IconComponent name={iconName} size={25} color={tintColor} />;
    };
    
    export default createAppContainer(
      createBottomTabNavigator(
        {
          Home: { screen: HomeScreen },
          Settings: { screen: SettingsScreen },
        },
        {
          defaultNavigationOptions: ({ navigation }) => ({
            tabBarIcon: ({ focused, tintColor }) =>
              getTabBarIcon(navigation, focused, tintColor),
          }),
          tabBarOptions: {
            activeTintColor: 'tomato',
            inactiveTintColor: 'gray',
          },
        }
      )
    );
    

    【讨论】:

    • 您的代码工作正常,但是我想要实现它的方式有点不同。我想把它放在它的 html 部分而不是标签栏
    • @sobhanmozafari 你的意思是你要显示的页面是html?然后,当您单击按钮时,您可以通过 WebView 显示您想要的页面。
    • 我不想使用 webview.im 使用 react native。你的意思是一些薄薄的pwa。
    猜你喜欢
    • 2015-04-16
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多