【问题标题】:react-native-tab-view is not visible , not displayed on screen or not workingreact-native-tab-view 不可见,不显示在屏幕上或不工作
【发布时间】:2022-11-10 08:47:06
【问题描述】:

react-native-tab-view 不可见,不显示在屏幕上或由于 flex: 1 样式不工作,我们必须在父视图中提供 flex: 1 样式,否则它不会显示在屏幕上。

供参考:https://github.com/satya164/react-native-tab-view/blob/main/example/src/CustomTabBarExample.tsx

解决方案 :-

第一个前。使用默认标签栏 -

import { StyleSheet, Text, View, useWindowDimensions } from 'react-native'
import React from 'react'

import { TabView, TabBar, SceneMap } from 'react-native-tab-view';

const BookingScreen = () => {

const layout = useWindowDimensions();

const FirstRoute = () => (
    <View style={{ flex: 1, backgroundColor: '#ff4081' }} /> // here also writing flex: 1 is mandatory
);

const SecondRoute = () => (
    <View style={{ flex: 1, backgroundColor: '#673ab7' }} /> // here also writing flex: 1 is mandatory
);



const [index, setIndex] = React.useState(0);
const [routes, setRoutes] = React.useState([
    { key: 'first', title: 'First' },
    { key: 'second', title: 'Second' },
]);

const renderScene = SceneMap({
    first: FirstRoute,
    second: SecondRoute,
    // first: FeedbackScreen,
    // second: ProfileScreen,
});





return (
    <>
        <View style={{ flex: 1 }}> // wrtie flex: 1 inside parent view its mandatory ***



            <TabView
                navigationState={{ index, routes }}
                renderScene={renderScene}
                onIndexChange={setIndex}
                initialLayout={{ width: layout.width }}
            />

            


        </View>

    </>
)
}

export default BookingScreen

第二个前。带有自定义标签栏 -

import { StyleSheet, Text, View, useWindowDimensions } from 'react-native'
import React from 'react'

import { TabView, TabBar, SceneMap } from 'react-native-tab-view';


const BookingScreen = () => {


const [index, setIndex] = React.useState(0);
const [routes, setRoutes] = React.useState([
    { key: 'first', title: 'First' },
    { key: 'second', title: 'Second' },
]);



const _renderTabBar = props => (
    <TabBar
        {...props}
        style={{ backgroundColor: '#fafafa' }}
        tabStyle={[styles.tab, { height: 38, maxHeight: 38, minHeight: 38, padding: 0 }]}
        labelStyle={[styles.label, { color: colors.text, }]}
        indicatorStyle={{ backgroundColor: 'blue' }}
    />
);



return (
    <>
        <View style={{ flex: 1 }}> // giving flex:1 inside parent view is mandatory


            

            <TabView
                navigationState={{ index, routes }}

                renderTabBar={_renderTabBar}
                onIndexChange={setIndex}
                renderScene={({ route }) => {
                    switch (route.key) {
                        case 'first':
                            return (<>
                                <View style={{ flex: 1, backgroundColor: '#ff4081' }} > // giving flex:1 is mandatory otherwise content will not shown
                                    <Text style={{ color: 'black' }}>sadfghjkljhgfcgvhbj</Text>
                                </View>

                            </>);

                        case 'second':
                            return (<>
                                <View style={{ flex: 1, backgroundColor: '#673ab7' }} > // giving flex:1 is mandatory otherwise content will not shown
                                    <Text style={{ color: 'black' }}>sadfghjkljhgfcgvhbj</Text>
                                </View>

                            </>);
                        default:
                            return null;


                    }
                }}
            />



        </View>

    </>
)
}

export default BookingScreen

const styles = StyleSheet.create({
    label: {
        fontWeight: '600',
        // fontSize: 12,
        flexWrap: 'wrap',
        // color: Colors.text,
        // minWidth: Dimensions.get('window').width < 375 ? '100%' : '60%'
    },
})

【问题讨论】:

    标签: react-native react-native-tab-view


    【解决方案1】:

    删除父容器将解决此问题。

    return (
                <TabView
                    navigationState={{ index, routes }}
                    renderScene={renderScene}
                    onIndexChange={setIndex}
                    initialLayout={{ width: layout.width }}
                />
    )
    

    【讨论】:

      【解决方案2】:

      对于未来的读者,一个常见的问题是父容器没有定义的高度。

      您必须设置父容器高度, 例如:

      <View style={{height: "100%"}}> //or flex: 1
          <TabView
              navigationState={{ index, routes }}
              renderScene={renderScene}
              onIndexChange={setIndex}
              initialLayout={{ width: layout.width }}
          />
      </View>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-26
        • 2022-06-13
        • 1970-01-01
        • 1970-01-01
        • 2020-11-25
        • 1970-01-01
        • 2014-03-30
        • 1970-01-01
        相关资源
        最近更新 更多