简体   繁体   English

我的 headerStyle 不起作用。 我试图分配背景颜色,但在我运行它时它没有出现

[英]My headerStyle doesn't work. I have tried to assign a background color though it doesn't appear when I run it

My headerStyle doesn't work.我的 headerStyle 不起作用。 I have tried to assign a background color though it doesn't appear when I run it.我试图分配背景颜色,但在我运行它时它没有出现。 I have given a blue color to backgroundColor in headerStyle.我在 headerStyle 中为 backgroundColor 赋予了蓝色。 though it isn't working.虽然它不工作。 I have tried everything but it doesn't work.我已经尝试了一切,但没有用。 I am fairly new to reactNative so cannot figure out the bug.我是 reactNative 的新手,所以无法找出错误。

import React, { Component } from 'react';
import Menu from './MenuComponent';
import Dishdetail from './DishdetailComponent';
import Home from './HomeComponent.js';
import Contact from './ContactComponent.js';
import About from './AboutComponent.js';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import { createDrawerNavigator } from 'react-navigation-drawer';
import { Icon } from 'react-native-elements';

const MenuNavigator = createStackNavigator({
  Menu: 
  { screen: Menu,
    navigationOptions: ({ navigation }) => ({
      headerLeft: <Icon name="menu" size={24} 
        color= 'blue'
        onPress={ () => navigation.toggleDrawer() } />          
    }) 
  },
  Dishdetail: { screen: Dishdetail }
},
{
  initialRouteName: 'Menu',
  navigationOptions: {
      headerStyle: {
          backgroundColor: "#512DA8"
      },
      headerTintColor: '#fff',
      headerTitleStyle: {
          color: "#fff"            
      }
  }
}
);

const HomeNavigator = createStackNavigator({
  Home: { screen: Home }
}, {
  navigationOptions: ({ navigation }) => ({
    headerStyle: {
      backgroundColor: '#f4511e',
    },
    headerTintColor: '#fff',
    headerTitleStyle: {
      fontWeight: 'bold',
    },
  })
});

const ContactNavigator = createStackNavigator({
  Contact: { screen: Contact }
}, 
{
  navigationOptions: ({ navigation }) => ({
    headerStyle: {
        backgroundColor: "#512DA8"
    },
    headerTitleStyle: {
        color: "#fff"            
    },
    headerLeft: <Icon name = 'menu' size = {24} 
        color = 'blue'
        onPress = {() => navigation.toggleDrawer()}
        />,
    headerTintColor: "#fff"  
  })
});

const AboutNavigator = createStackNavigator({
  About: { screen: About }
}, 
{
  navigationOptions: ({ navigation }) => ({
    headerStyle: {
        backgroundColor: "blue"
    },
    headerTitleStyle: {
        color: "#fff"            
    },
    headerLeft: <Icon name = 'menu' size = {24} 
        color = 'blue'
        onPress = {() => navigation.toggleDrawer()}
        />,
    headerTintColor: "blue"  
  })
});

const MainNavigator = createDrawerNavigator({
  Home: { 
      screen: HomeNavigator,
      navigationOptions: {
        title: 'Home',
        drawerLabel: 'Home',
        drawerIcon: ({tintColor}) => (
          <Icon 
            name='home' 
            type='font-awesome'
            size = {24}
            color = {tintColor}
          />
        )
      }
    },
  About: { 
      screen: AboutNavigator,
      navigationOptions: {
        title: 'About Us',
        drawerLabel: 'About Us',
        drawerIcon: ({tintColor}) => (
          <Icon 
            name='info-circle' 
            type='font-awesome'
            size = {24}
            color = {tintColor}
          />
        )
      }, 
    },
  Menu: { 
      screen: MenuNavigator,
      navigationOptions: {
        title: 'Menu',
        drawerLabel: 'Menu',
        drawerIcon: ({tintColor}) => (
          <Icon 
            name='list' 
            type='font-awesome'
            size = {24}
            color = {tintColor}
          />
        )
      }, 
    },
  Contact: { 
      screen: ContactNavigator,
      navigationOptions: {
        title: 'Contact Us',
        drawerLabel: 'Contact Us',
        drawerIcon: ({tintColor}) => (
          <Icon 
            name='address-card' 
            type='font-awesome'
            size = {22}
            color = {tintColor}
          />
        )
      }, 
    }
}, {
drawerBackgroundColor: '#D1C4E9'
});

const Main = createAppContainer(MainNavigator);

export default Main;

Depending on what version of React Native you are using the issue could be with navigationOptions .根据您使用的 React Native 版本,问题可能与navigationOptions有关。 If you are using v3 you need to use defaultNavigationOptions instead.如果您使用的是 v3,则需要改用defaultNavigationOptions

defaultNavigationOptions: ({ navigation }) => ({
    headerStyle: {
        backgroundColor: "blue"
    },
    ...
}

I am currently facing the same issue... React native v0.70.5 expo managed project created with typescript.我目前面临同样的问题... React native v0.70.5 expo 托管项目使用 typescript 创建。

in my case, only background color works every other style property isn't expected to be props of header style, I looked into their package, and here's what I found...在我的例子中,只有背景颜色起作用,其他样式属性预计不会成为 header 样式的道具,我查看了他们的 package,这就是我发现的...

headerStyle?: StyleProp<{
    backgroundColor?: string;
}>;

in this file.. node_modules@react-navigation\native-stack\src\types.tsx在这个文件中.. node_modules@react-navigation\native-stack\src\types.tsx

for me the most reasonable solution is to add the properties as defined above.对我来说,最合理的解决方案是添加上面定义的属性。

So I've added the styles I need...所以我添加了我需要的 styles ...

  headerStyle?: StyleProp<{
    backgroundColor?: string;
    borderBottomWidth: number,
    shadowColor: string,
    shadowOpacity: number,
    elevation: number,
    height: number
 }>;

or you could just say:或者你可以说:

  headerStyle?: StyleProp<ViewStyle>;

You might need to restart vscode though before the changed reflect... but this solved it for me.您可能需要在更改反映之前重新启动 vscode...但这为我解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我已经尝试过,但是我的程序似乎不起作用 - I have tried but my program doesn't seem to work 如果我没有嵌套if / else语句,则代码不起作用。 我为什么要窝? - If I don't nest the if/else statements, the code doesn't work. Why do I have to nest? 我试图用 fetch 更新我的 ejs 页面,但它不起作用 - i tried to upate my ejs page with fetch but it doesn't work 我的机器人不和谐不起作用。 我不知道为什么 - My bot discord doesn't work. I don't know why 我刚刚格式化数字以逗号显示,但现在我的计算器不起作用? - I have just formatted numbers to appear with commas but now my calculator doesn't work? 我有两个具有相同代码的不同域,但是其中一个不起作用。 为什么? - I have two different domains with the same code, but one of them doesn't work. Why? 带有偏移的边栏不起作用。 我怎么了 - Sidebar with offset doesn't work. What do I wrong? 当我单击“添加”按钮时,我想添加文本字段。 但它不起作用。 帮我 - I want to add text field when i clicked the button “add”. But it doesn't work. Help me 我试图在浏览器中制作一个画家,但后来它制作了一个“橡皮擦”,但它不起作用,因为它不会改变颜色 - I tried to make a painter in browser, but then it went to making an 'eraser' it didn't work because it doesn't change the color 我的 JavaScript 不起作用。 我怎么解决这个问题? - My JavaScript doesn't work. How can I solve this problem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM