【问题标题】:tabBarOptions not applied to project (React Native)tabBarOptions 未应用于项目(React Native)
【发布时间】:2022-02-12 01:42:53
【问题描述】:

我正在创建一个包含待办事项列表和日历的小应用程序。底部是底部标签导航器。一切正常,但是,当我尝试在tabBarOptions 中添加style: {} 时,它没有被应用。更改 activeTintColorinactiveTintColorlabelStyle 效果很好。

我尝试创建一个样式表并替换 tabBarOptions 中的所有内容,但这不起作用。我的主要目标是简单地在屏幕底部创建一个稍大的条。我什至不想要一个疯狂的自定义导航栏,只是稍微大一点,这样我就可以让里面的项目更大一点。

MainContainer 类:

import React from 'react';
import {StyleSheet} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import Ionicons from 'react-native-vector-icons/Ionicons'

//screens 
import Calendar from './screens/Calendar';
import ToDoList from './screens/ToDoList';

// Screen names
const calendarName = 'Calendar';
const ToDoListName = 'To Do List';

const Tab = createBottomTabNavigator();

export default function MainContainer() {
    return (
        <NavigationContainer>
            <Tab.Navigator
                tabBarOptions={{
                    activeTintColor: 'tomato',
                    inactiveTintColor: 'black',
                    labelStyle: {paddingBottom: 10, fontSize: 10},
                    style: {padding: 10, height: 70},
                }}
                initialRouteName={ToDoListName}
                           screenOptions={({route}) => ({
                               tabBarIcon: ({focused, color, size}) => {
                                   let iconName;
                                   let rn = route.name;

                                   if (rn === ToDoListName) {
                                       iconName = focused ? 'list' : 'list-outline'; //icons in package. Change later.
                                   } else if (rn === calendarName) {
                                       iconName = focused ? 'calendar' : 'calendar-outline'
                                   }
                                   return <Ionicons name={iconName} size={size} color={color}/>
                               },
                           })}>

                <Tab.Screen name={ToDoListName} component={ToDoList}/>
                <Tab.Screen name={calendarName} component={Calendar}/>

            </Tab.Navigator>
        </NavigationContainer>
    )
}

这里是我的 ToDoList 类的参考

import { KeyboardAvoidingView, StyleSheet, Text, View, TextInput, TouchableOpacity, Platform, Keyboard } from 'react-native';
import Task from '../../components/Task';
import React, { useState } from 'react';
import { ScrollView } from 'react-native-web';


export default function ToDoList() {
    const [task, setTask] = useState();
    const [taskItems, setTaskItems] = useState([]);

    const handleAddTask = () => {
        Keyboard.dismiss();
        setTaskItems([...taskItems, task])
        setTask(null);
    }


    const completeTask = (index) => {
        let itemsCopy = [...taskItems];
        itemsCopy.splice(index, 1);
        setTaskItems(itemsCopy)
    }

    return (
        <View style={styles.container}>
            {/* Scroll View when list gets longer than page */}
            <ScrollView contentContainerStyle={{
                flexGrow: 1
            }} keyboardShouldPersistTaps='handled'>

                {/*Today's Tasks */}
                <View style={styles.tasksWrapper}>
                    <Text style={styles.sectionTitle}>Today's Tasks</Text>
                    <View style={styles.items}>
                        {/* This is where the tasks will go*/}
                        {
                            taskItems.map((item, index) => {
                                return (
                                    <TouchableOpacity key={index} onPress={() => completeTask(index)}>
                                        <Task text={item} />
                                    </TouchableOpacity>
                                )
                            })
                        }
                    </View>
                </View>

            </ScrollView>

            {/* Write a task section */}
            {/* Uses a keyboard avoiding view which ensures the keyboard does not cover the items on screen */}
            <KeyboardAvoidingView
                behavior={Platform.OS === "ios" ? "padding" : "height"}
                style={styles.writeTaskWrapper}>
                <TextInput style={styles.input} placeholder={'Write a task'} value={task} onChangeText={text => setTask(text)} />
                <TouchableOpacity onPress={() => handleAddTask()}>
                    <View style={styles.addWrapper}>
                        <Text style={styles.addText}>+</Text>
                    </View>
                </TouchableOpacity>
            </KeyboardAvoidingView>
        </View>
    );
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#E8EAED',
    },
    tasksWrapper: {
        paddingTop: 20,
        paddingHorizontal: 20,
    },
    sectionTitle: {
        fontSize: 24,
        fontWeight: 'bold',
    },
    items: {
        marginTop: 30,
    },
    writeTaskWrapper: {
        position: 'absolute',
        bottom: 20,
        paddingLeft: 10,
        paddingRight: 10,
        width: '100%',
        flexDirection: 'row',
        justifyContent: 'space-between',
        alignItems: 'center'
    },
    input: {
        paddingVertical: 15,
        width: 250,
        paddingHorizontal: 15,
        backgroundColor: '#FFF',
        borderRadius: 60,
        borderColor: '#C0C0C0',
        borderWidth: 1,
    },
    addWrapper: {
        width: 60,
        height: 60,
        backgroundColor: '#FFF',
        borderRadius: 60,
        justifyContent: 'center',
        alignItems: 'center',
        borderColor: '#C0C0C0',
        borderWidth: 1,
    },
    addText: {

    },

});

还有我的日历课

import * as React from 'react';
import { View, Text } from 'react-native';
export default function Calendar(){

    return(
        <View>
            <Text>Calendar Will go here</Text>
        </View>
    )

}

我为 ToDoList 创建了一个任务组件。不知道你是否需要它来解决这个问题,但我还是把它贴在这里。

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { TouchableOpacity } from 'react-native-web';

const Task = (props) => {
    return (
        <View style={styles.item}>
            <View style={styles.itemLeft}>
                <View style={styles.square}></View>
                <Text style={styles.itemText}>{props.text}</Text>
            </View>
            <View style={styles.circular}></View>
        </View>
    )
}

const styles = StyleSheet.create({
    item: {
        backgroundColor: '#FFF',
        padding: 15,
        borderRadius: 10,
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
        marginBottom: 20,

    },
    itemLeft: {
        flexDirection: 'row',
        alignItems: 'center',
        flexWrap: 'wrap',
    },
    square: {
        width: 24,
        height: 24,
        backgroundColor: '#55BCF6',
        opacity: .4,
        borderRadius: 5,
        marginRight: 15,
    },
    itemText: {
        maxWidth: '80%',

    },
    circular: {
        width: 12,
        height: 12,
        borderColor: '#55BCF6',
        borderWidth: 2,
        borderRadius: 5
    },
});

export default Task;

【问题讨论】:

    标签: javascript css reactjs react-native


    【解决方案1】:

    听起来您正在寻找tabBarStyle 属性。您应该能够将 style(这不是选项卡导航器支持的属性)重命名为 tabBarStyle

    这是文档中提到这一点的地方。 https://reactnavigation.org/docs/bottom-tab-navigator#tabbarstyle

    【讨论】:

      【解决方案2】:

      我最终解决这个问题的方法是将样式放在 screenOptions 中。我不想这样做,因为我想将样式与逻辑分开,但它为我解决了问题。见以下代码:

      export default function MainContainer() {
          return (
              <NavigationContainer>
                  <Tab.Navigator
      
                      initialRouteName={ToDoListName}
                                 screenOptions={({route}) => ({
                                     tabBarIcon: ({focused, color, size}) => {
                                         let iconName;
                                         let rn = route.name;
      
                                         if (rn === ToDoListName) {
                                             iconName = focused ? 'list' : 'list-outline'; //icons in package. Change later.
                                         } else if (rn === calendarName) {
                                             iconName = focused ? 'calendar' : 'calendar-outline'
                                         }
                                         return <Ionicons name={iconName} size={size} color={color}/>
                                     },
                                     activeTintColor: 'tomato',
                                     inactiveTintColor: 'black',
                                     tabBarShowLabel: false,
                                     tabBarStyle: {padding: 10, height: 100, backgroundColor: 'black'},
                                 })}>
      
                      <Tab.Screen name={ToDoListName} component={ToDoList}/>
                      <Tab.Screen name={calendarName} component={Calendar}/>
      
                  </Tab.Navigator>
              </NavigationContainer>
          )
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-07
        • 2022-11-11
        • 2019-03-17
        • 1970-01-01
        • 2022-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多