Creating a header in react-native and nesting multiple tabs within it

I am new to react-native, and I’m trying to design a header for a screen that incorporates a back button, a title, and two tabs. While I have a bottom tab layout that applies to all my screens, I simply want to create a top tab layout that can be nested within the current screen I am on.

My problem is rather weird, if I do not nest the Tab.navigator within my header view, the tabs render just fine, although there is a blank space in between. It is why I am trying to nest my tabs within the header. However, when I attempt to do so, the rendering gets weird, with the tabs extending past the size of the screen?

For example, when I use alignItems: ‘center’ to style my header view, it results in the first imagethe two tabs are split in half. If I try alignItems: ‘flex-start’, only the left tab is shown, while if I try alignItems: ‘flex-end’, only the right tab is shown . Notably, if I do not use alignItems, the tabs do not show up at all. Below is my code for the screen, could someone explain why such an error occurs, and how I can fix it?

import { View, Text, Button, StyleSheet, TextInput, ScrollView, TouchableOpacity, Platform } from 'react-native';
import { useNavigation, NavigationContainer } from '@react-navigation/native';
import { HeaderBackButton } from '@react-navigation/elements';
import DateTimePicker, { DateTimePickerEvent, Event } from '@react-native-community/datetimepicker';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';

const Tab = createMaterialTopTabNavigator();

function FirstTab() {
  return (
    <View style={{...styles.container}}>
      <Text>First Tab Content</Text>
      <View>
        <Text style={styles.descText}>Title</Text>
        <TextInput style={styles.inputText}></TextInput>

        <Text style={styles.descText}>Paid By</Text>
        <TextInput style={styles.inputText}></TextInput>
      </View>
    </View>
  );
}

function SecondTab() {
  return (
    <View style={styles.container}>
      <Text>Second Tab Content</Text>
    </View>
  );
}

export default function group() {
  const navigation = useNavigation();

  const handleBackButtonPress = () => {
    navigation.goBack();
  };


  return (
    <View style={styles.container}>
      <View style={styles.header}>
        <View style={{flexDirection: 'row', borderWidth: 2}}>
          <HeaderBackButton tintColor='white' onPress={handleBackButtonPress} />
          <Text style={styles.headerText}>#Group-Name-Here</Text>
        </View>
        <View>   
          <Tab.Navigator 
            screenOptions={({ route }) => ({
            tabBarLabelStyle: { fontSize: 16, color: 'white' },
            tabBarStyle: { backgroundColor: 'purple' },
            tabBarIndicatorStyle: { backgroundColor: 'white' },
            tabBarLabel: ({ focused }) => {
              let label;
              if (route.name === 'FirstTab') {
              label = 'First';
              } else if (route.name === 'SecondTab') {
              label = 'Second';
              }
            return <Text style={{ color: focused ? 'white' : 'gray' }}>{label}</Text>;
            },
          })}>
            <Tab.Screen name="FirstTab" component={FirstTab} options={{ tabBarLabel: 'Bills' }} />
            <Tab.Screen name="SecondTab" component={SecondTab} options={{ tabBarLabel: 'Balances' }} />
          </Tab.Navigator>
        </View>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'black',
  },
  header: {
    backgroundColor: 'purple',
    flexDirection: 'column',
    alignItems: 'center', //THE LINE THAT CAUSES WEIRD ISSUES?
    padding: 8,
    marginBottom: 12,
    marginTop: 50,
  },
  headerText: {
    // textAlign: 'center',
    fontSize: 24,
    color: 'white',
    marginLeft: 8,
  },
  descText: {
    color: 'grey',
    fontSize: 16,
    padding: 8,
    marginHorizontal: 24,
  },
  inputText: {
    color: 'white',
    paddingHorizontal: 8,
    fontSize: 20,
    marginHorizontal: 24,
    borderBottomColor: 'purple',
    borderWidth: 2,
    marginBottom: 8,
  },
});

New contributor

YF Leong is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật