Как создать прозрачный фон в реагирующей навигации 5.x? - PullRequest
3 голосов
/ 02 апреля 2020

Я изменил цвет фона, чтобы сделать его более заметным. Я хочу, чтобы красный контейнер стал прозрачным. enter image description here

Есть ли способ добиться этого? Я использую реагирующую навигацию 5 и создал собственную нижнюю панель вкладок в соответствии с Bottom-tab-navigator

Код, который я использую для нижней панели, следующий

import React from 'react';
import { View, StyleSheet } from 'react-native';
import colors from 'styles/colors';
import TabBarItem from './TabBarItem/TabBarItem';
const homeIcon = require('assets/maps.png');

export enum Item {
  Home,
  ProfileView,
}

const DashboardTabBar: React.FC<{}> = ({ state, descriptors, navigation }) => {
  return (
    <View style={styles.container}>
      <View style={styles.innerContainer}>
        {state.routes.map((route, index) => {
          const { options } = descriptors[route.key];

          const isFocused = state.index === index;

          const onPress = () => {
            const event = navigation.emit({
              type: 'tabPress',
              target: route.key,
            });

            if (!isFocused && !event.defaultPrevented) {
              navigation.navigate(route.name);
            }
          };

          const onLongPress = () => {
            navigation.emit({
              type: 'tabLongPress',
              target: route.key,
            });
          };

          return (
            <TabBarItem
              icon={homeIcon}
              isFocused={isFocused}
              onPress={onPress}
              onLongPress={onLongPress}
              options={options}
              key={route.key}
            />
          );
        })}
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    padding: 10,
    paddingBottom: 18,
    backgroundColor: 'red', // I tried with 'transparent' here, but i see a white background instead of transparent
  },
  innerContainer: {
    height: 60,
    backgroundColor: colors.GREY_L_10,
    flexDirection: 'row',
    justifyContent: 'space-evenly',
    alignItems: 'center',
    borderRadius: 50,
    borderWidth: 1,
    borderColor: colors.GREY,
  },
});

export default DashboardTabBar;

Насколько я посмотрел в коде, я не мог найти способ сделать его прозрачным.

Ответы [ 3 ]

1 голос
/ 05 апреля 2020

Где-то еще в вашем коде у вас есть компонент, который использует ваш компонент DashboardTabBar. Вы должны добавить tabBarOptions prop с объектом style к компоненту Tab.Navigator , например:

    <Tab.Navigator
      tabBar={...}
      tabBarOptions={{
        style: {
          backgroundColor: 'transparent',
          position: 'absolute',
          left: 0,
          right: 0,
          bottom: 0,
          elevation: 0, <----to get rid of a shadow problem on Android
        },
      }}>
    { /* ...your screens go here */ }
</Tab.Navigator>

Я успешно сделал это как для iOS, так и для Android. Лично для моего приложения это выглядит не очень хорошо. enter image description here enter image description here

0 голосов
/ 05 апреля 2020

По умолчанию Navigator, возвращенный из createBottomTabNavigator, не перекрывает экраны с помощью TabBar. При этом, хотя ваш TabBar прозрачен, ваш экран заканчивается там, где начинается TabBar. Экран не go позади TabBar

К счастью, все, что вам нужно сделать, это добавить position: 'absolute', bottom: 0, left: 0, right: 0 в ваш стиль TabBar container (тот, который вы применяете backgroundColor: 'transparent' к)

0 голосов
/ 02 апреля 2020
const styles = StyleSheet.create({
  container: {
    padding: 10,
    paddingBottom: 18,
    backgroundColor: 'transparent',
  },
  innerContainer: {
    height: 60,
    backgroundColor: colors.GREY_L_10,
    flexDirection: 'row',
    justifyContent: 'space-evenly',
    alignItems: 'center',
    borderRadius: 50,
    borderWidth: 1,
    borderColor: colors.GREY,
  },
});
...