React-навигация: как оформить большую среднюю кнопку в навигации по вкладкам? - PullRequest
1 голос
/ 19 апреля 2019

Итак, у меня есть это требование.Чтобы сделать навигацию по вкладкам с таким точным внешним видом:

This is the goal У меня не было проблем с оформлением панели вкладок с помощью градиента и кнопок.Я создал свой собственный с этим кодом:

export default createBottomTabNavigator({
  (... routes here)
}, {
  initialRouteName: "Inspiration",
  tabBarComponent: props => <BottomTabBar {...props} />
})

Но теперь у меня проблемы с средней кнопкой.Моя панель выглядит следующим образом:

This is how it looks now

Если я удаляю пользовательскую панель вкладок, удаляя эту строку:

tabBarComponent: props => <BottomTabBar {...props} />

Тогда теперь мойсредняя кнопка выглядит так, как должна, но, конечно, все остальные стили отсутствуют:

Not close enough

Вот так выглядит мой компонент BottomTabBar прямо сейчас:

import React from "react";
import { Image, StyleSheet, Text, TouchableOpacity } from "react-native";
import { TabBarBottomProps, NavigationRoute } from "react-navigation";
import LinearGradient from "react-native-linear-gradient";

const iconBag = require("./images/bag.png");

export default function BottomTabBar(props: TabBarBottomProps) {
  const {
    renderIcon,
    getLabelText,
    activeTintColor,
    inactiveTintColor,
    onTabPress,
    onTabLongPress,
    getAccessibilityLabel,
    navigation
  } = props;

  const { routes, index: activeRouteIndex } = navigation.state;

  function renderTabBarButton(routeIndex, route) {
    const isRouteActive = routeIndex === activeRouteIndex;
    const tintColor = isRouteActive ? activeTintColor : inactiveTintColor;

    if (route.key == "Bag")
      return <Image style={{ width: 100, height: 100 }} source={iconBag} />;
    return (
      <TouchableOpacity
        key={routeIndex}
        style={styles.tabButton}
        onPress={() => onTabPress({ route })}
        onLongPress={() => onTabLongPress({ route })}
        accessibilityLabel={getAccessibilityLabel({ route })}
      >
        {renderIcon({ route, focused: isRouteActive, tintColor })}
        <Text style={styles.tabText}>{getLabelText({ route })}</Text>
      </TouchableOpacity>
    );
  }

  return (
    <LinearGradient colors={["#FFFFFF", "#DEDEDE"]} style={styles.container}>
      {routes.map((route, routeIndex) => renderTabBarButton(routeIndex, route))}
    </LinearGradient>
  );
}

const styles = StyleSheet.create({
  container: {
    height: 60,
    flexDirection: "row",
    alignItems: "center",
    borderWidth: 1,
    borderColor: "#C4C4C4"
  },
  tabButton: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center"
  },
  tabText: {
    fontFamily: "Source Sans Pro",
    fontSize: 11,
    color: "#454545",
    marginTop: 1
  }
});

Что я могу сделать?Любая помощь будет очень ценится!

1 Ответ

2 голосов
/ 19 апреля 2019

enter image description here

Я сделал эту нижнюю вкладку с реагировать родной. Я думаю, что ваш дизайн очень прост. Думаю, мой пример кода поможет вам.

import React from 'react';
import { createBottomTabNavigator, createStackNavigator } from 'react-navigation';
import { View, Image } from 'react-native'
import { Text } from 'native-base';

import Featured from './featured';
import AboutUs from './about_us';
import Shop from './shop';
import Booking from './booking';
import Settings from './settings';
import styles from './styles';

import star from './../../assets/images/icons/star.png';
import star_check from './../../assets/images/icons/star_check.png';
import about from './../../assets/images/icons/about.png';
import about_check from './../../assets/images/icons/about_check.png';
import book from './../../assets/images/icons/book.png';
import check from './../../assets/images/icons/check.png';
import shop from './../../assets/images/icons/shop.png';
import shop_check from './../../assets/images/icons/shop_check.png';
import more from './../../assets/images/icons/more.png';
import more_check from './../../assets/images/icons/more_check.png';

const Tabs = createBottomTabNavigator(
    {
        Featured: {
            screen: Featured,
            navigationOptions: {
                title: 'Featured',
                tabBarIcon: ({ tintColor, focused }) => (
                        <View style={styles.tab}>
                            <Image source={focused? star_check : star} style={styles.icon} />
                            <Text style={[styles.name, {color: tintColor}]}>Kampanjer</Text>
                        </View>
                    )
            }
        },
        AboutUs: {
            screen: AboutUs,
            navigationOptions: {
                title: 'About Us',
                tabBarIcon: ({ tintColor, focused }) => (
                            <View style={styles.tab}>
                                <Image source={focused? about_check : about} style={styles.icon} />
                                <Text style={[styles.name, {color: tintColor}]}>Om oss</Text>
                            </View>
                        )
            }
        },
        Booking: {
            screen: Booking,
            navigationOptions: {
                title: 'MIN SALONG',
                tabBarIcon: ({ tintColor, focused }) => (
                            <View style={styles.book}>
                                <Image source={focused? check : book} style={styles.book_icon} />
                            </View>
                        )
            }
        },
        Shop: {
            screen: Shop,
            navigationOptions: {
                title: 'Shop',
                tabBarIcon: ({ tintColor, focused }) => (
                            <View style={styles.tab}>
                                <Image source={focused? shop_check : shop} style={styles.icon} />
                                <Text style={[styles.name, {color: tintColor}]}>Shop</Text>
                            </View>
                        )
            }
        },
        Settings: {
            screen: Settings,
            navigationOptions: {
                title: 'More',
                tabBarIcon: ({ tintColor, focused }) => (
                            <View style={styles.tab}>
                                <Image source={focused? more_check : more} style={styles.icon} />
                                <Text style={[styles.name, {color: tintColor}]}>Inställningar</Text>
                            </View>
                        )
            }
        },
    },
    {
        initialRouteName: 'Featured',
        tabBarOptions: {
            activeTintColor: '#80A0AB',
            inactiveTintColor: '#fff',
            showLabel: false,
            style: {
                height: 60,
                backgroundColor: '#485155'
            },
            labelStyle: {
                fontSize: 12,
                fontFamily: 'Abel-Regular'
            }
        }  
    }
);

export default createStackNavigator({Tabs}, {headerMode: "none"});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...