【问题标题】:React navigation- Wrap button in TabBar反应导航- TabBar 中的换行按钮
【发布时间】:2018-06-23 23:31:36
【问题描述】:

我是 React Native 的新手,我正在尝试在图像中使用 TabBar。我的问题是在标签栏中放一个按钮。如果有人可以帮助我或有一个想法来创建这个标签栏,那就太好了。 谢了

【问题讨论】:

标签: react-native react-navigation tabbar


【解决方案1】:

你可以检查 这个link。通过 TabNavigator 的道具之一是tabBarComponent。如果您不想要默认样式或必须自定义 tabBar,您可以指定组件的外观。

在您的情况下,这应该可行。

import React from 'react';
import {View, Text, TouchableOpacity, Dimensions} from 'react-native';
import {TabNavigator} from 'react-navigation';
import Tab1Screen from '../components/tab1Screen';
import Tab2Screen from '../components/tab2Screen';

var {height, width} = Dimensions.get('window');

const mainRoutes = TabNavigator({
 Tab1: {screen: Tab1Screen},
 Tab2: {screen: Tab2Screen}
},
{
 tabBarComponent:({navigation}) => (
  <View style={{flex: 0.1, borderColor: 'green', borderWidth: 1}}>
    <View style={{flexDirection:'row', justifyContent: 'space-around', alignItems: 'center', paddingTop: 15}}>
      <View style={{width: 40, height: 40, borderRadius: 20, borderColor: 'red', borderWidth: 1, position: 'absolute', left: width/2.5, bottom:13 }}></View>
      <TouchableOpacity onPress={() => navigation.navigate('Tab1')}>
        <Text>Tab1</Text>
      </TouchableOpacity>
      <TouchableOpacity onPress={() => navigation.navigate('Tab2')}>
        <Text>Tab2</Text>
      </TouchableOpacity>
    </View>
</View>
)});

export default mainRoutes;

【讨论】:

  • 你在设备上试过了吗?我有你的代码,但它不工作
【解决方案2】:

我不得不做类似的事情。我正在使用 React Navigation v5,在我的情况下,按钮必须执行自定义操作,而不是导航到选项卡。所以这就是我所做的:

import styles from './styles';
// Other needed imports...

// ...

<Tab.Screen
  name="Add Recipe"
  component={() => null}
  options={{
    tabBarButton: () => (
      <TouchableOpacity
        style={styles.addIconWrapper}
        onPress={() => {
          // Your custom action here
        }}
      >
        <View style={styles.addIconBackground}>
          <Image source={assets.add} style={styles.addIconImage} />
        </View>
      </TouchableOpacity>
    ),
  }}
/>;

在styles.js里面:

 // ...

addIconWrapper: {
    width: '20%',
    justifyContent: 'center',
    alignItems: 'center',
  },

  addIconBackground: {
    marginTop: -30,
    backgroundColor: '#85c349',
    borderColor: 'white',
    shadowOffset: { width: 2, height: 2 },
    shadowColor: '#999',
    shadowOpacity: 0.5,
    borderRadius: 1000,
    width: 42,
    height: 42,
    justifyContent: 'center',
    alignItems: 'center',
  },

  addIconImage: {
    tintColor: 'white',
  },

最终结果

【讨论】:

  • 非常感谢,我暂时找不到这个答案!
猜你喜欢
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 2019-08-01
  • 2017-08-07
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多