I'm trying to add a tabbed navigation to a React Native app, also using expo if that matters, and whenever I do this I get a big chunk of white at the top of the screen. I'm not seeing what's causing this though, the status bar background shouldn't be pushing it down and as far as I can tell changing the height style of my navigation or main view doesn't do anything.
This looks like:
The complete source to recreate this is:
// utils/colors.js
export const white = '#fff'
export const orange = '#f26f28'
// App.js
import React, { Component } from 'react'
import { StyleSheet, View, StatusBar, Dimensions } from 'react-native'
import {
createMaterialTopTabNavigator,
createStackNavigator
} from 'react-navigation'
import { Constants } from 'expo'
import List from './components/List'
import Add from './components/Add'
import { orange, white } from './utils/colors'
function StatusBarBackground ({ backgroundColor, ...props }) {
return (
<View style={{ backgroundColor, height: Constants.statusBarHeight }}>
<StatusBar translucent backgroundColor={backgroundColor} {...props} />
</View>
)
}
const Tabs = createMaterialTopTabNavigator({
List: {
screen: List,
navigationOptions: {
tabBarLabel: 'List',
},
},
Add: {
screen: Add,
navigationOptions: {
tabBarLabel: 'Add',
},
},
}, {
navigationOptions: {
header: null
},
tabBarOptions: {
activeTintColor: white,
style: {
height: 56,
backgroundColor: orange,
shadowColor: 'rgba(0, 0, 0, 0.24)',
shadowOffset: {
width: 0,
height: 3
},
shadowRadius: 6,
shadowOpacity: 1
}
}
})
const MainNavigator = createStackNavigator({
Home: {
screen: Tabs,
},
})
export default class App extends Component {
render() {
return (
<View style={{flex: 1}}>
<StatusBarBackground backgroundColor={orange} barStyle="light-content" />
<MainNavigator />
</View>
)
}
}
The Add and List components are just a view with a text, both look similar to this:
import React, { Component } from 'react'
import { View, Text } from 'react-native'
class Add extends Component {
render() {
return (
<View>
<Text style={{fontSize: 20}}>Add</Text>
</View>
)
}
}
export default Add
You are adding Your Tab Navigator inside Stack Navigator, That is why that white header is showing from Stack Navigator.
1) If You want to add header then style your header like below
const MainNavigator = createStackNavigator({
Home: {
screen: Tabs,
},
},{
navigationOptions:{
headerStyle : {
backgroundColor:'#243346'
},
headerTintColor:"#fff"
},
})
2) If you want to remove header then you can either remove your Tab Navigator from Stack Navigator or add headerMode:'none' inside navigationOprions object.
Related
I'm using react-navigation. I want createStackNavigator style navigation, but I just want the header to be at the bottom of the screen instead of the top. It seems the only way to get a bottom navbar without making my own navigator is with createBottomTabNavigator.
I'm making a flow, with one screen following another until it ends.
The headerStyle prop doesn't take positioning properties like bottom, top, left.
Following some advice here, this is now what I have.
import BottomStackNavigationView from '../components/BottomStackNavigationView'
export default class MyScreen extends Component {
static navigationOptions = {
title: 'giraffe',
headerTitle: 'monkey',
headerBackTitle: 'c3po',
header: (props) => <BottomStackNavigationView {...props} />,
}
render() {}
I log the props I get in my BottomStackNavigationView and it doesn't look like I am getting, for example, headerTitle, title, or headerBackTitle.
Is there a better way of doing this than making a custom navigator?
As mentioned by #Darpan Rangari in the comments, position: absolute is not available in headerStyle so the below solution should work.
Try this:
export default class MyScreen extends Component {
static navigationOptions = {
header: (navigationOptions) => (<View style={{position:"absolute", bottom: 0, height: 80, width: "100%", backgroundColor: "#dbdbdb"}}><Text>{navigationOptions.headerTitle}</Text></View>); //this is your custom header
render() {}
}
I needed a footer at the bottom of my stack navigator too. When bottom: 0 didn't work, I found the height of my screen, and used:
import React from 'react'
import { createStackNavigator } from 'react-navigation-stack'
import { Dimensions, View, Text } from 'react-native';
const styles = {
position: 'absolute',
width: '100%',
height: 130,
top: Dimensions.get('window').height-130,
backgroundColor: 'orange',
}
const A = props => (
<Text style={{width: '50%', height: 200, backgroundColor: 'green'}}
onPress={() => props.navigation.navigate('B')} />
)
const EventCreationStack = createStackNavigator(
{
A: {screen: A},
B: {screen: A},
},
{
defaultNavigationOptions: {
header: props => <View style={styles} />,
},
}
)
export default EventCreationStack
I'm trying to achieve a Instagram-like navigation
I have a buttom tab navigation in App.js
import React from 'react';
import { StyleSheet, Text, View, Image } from 'react-native';
import { createAppContainer, createBottomTabNavigator} from 'react-navigation';
import Ionicons from 'react-native-vector-icons/Ionicons';
import Home from './views/Home'
import Search from './views/Search'
const MainNavigator = createBottomTabNavigator({
Home: { screen: Home },
Search: { screen: Search },
}, {
defaultNavigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
let IconComponent = Ionicons;
let iconName;
if(routeName === 'Home'){
iconName = `ios-home`;
}
if(routeName === 'Search'){
iconName = `ios-search`;
}
return <IconComponent name={iconName} size={25} color={tintColor} />;
}
}),
initialRouteName: 'Search',
tabBarOptions: {
activeTintColor: '#fff',
activeBackgroundColor: '#4c399c',
inactiveTintColor: '#f1f3f5',
inactiveBackgroundColor: '#5442a0',
showLabel: false
},
});
const App = createAppContainer(MainNavigator);
export default App;
I'll have more than two views at the bottom.
The top bar will have a centered logo on all views, and some views will have 1 left button and/or 1 right button.
What I'm trying to achieve is to not render the header bar in every view, but to declare one globally (like the bottom navigation) and add the custom buttons on the few views that will have them
put the tab navigation as the parent, and add a stack navigation inside each tab.
An easy way to do this is using the Header component from react-native-elements. You just need to add this to the screen that you want the header on.
It's really easy to add the buttons to open drawer or anything else that you need.
For this to work, don't forget to add the header: null to the stack navigators, otherwise you'll end up with 2 headers on your screen.
Example below:
<React.Fragment>
<Header
statusBarProps={{ barStyle: 'light-content' }}
barStyle="light-content"
leftComponent={
<SimpleIcon
name="menu"
color="#34495e"
size={20}
/>
}
centerComponent={{ text: 'HOME', style: { color: '#34495e' } }}
containerStyle={{
backgroundColor: 'white',
justifyContent: 'space-around',
}}
/>
</React.Fragment>
This code will display centered the header on each screen of your BottomTabNavigator - MainNavigator
const AppNavigator = createStackNavigator({
Home: {screen: MainNavigator}
},
defaultNavigationOptions: {title: 'Instagram'},
headerLayoutPreset: 'center'
})
const App = createAppContainer(AppNavigator);
export default App;
I'm trying to open a navigation draw from a stack navigation header button. The header button is showing up fine but when I click the button I am getting
undefined is not an object (evaluating '_this.props.navigate')
I can't seem to find a solid example of how to do this or if its even possible with react navigation.
import React, { Component } from 'react';
import { createStackNavigator, NavigationActions } from 'react-navigation'
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import { Draw } from './DrawNav.js'
export const RootStack = createStackNavigator (
{
DrawNav: {
screen: Draw,
navigationOptions: ({ navigation }) => ({
//Hide the shadow of the header
headerStyle: {
elevation:0,
shadowColor: 'transparent',
shadowRadius: 0,
shadowOffset: {
height: 0,
}
},
headerLeft: (
<View style={{marginLeft: 10}}>
<Icon
name="menu"
size={25}
color="#D4AF37"
onPress={() => this.props.navigation.openDrawer()}
/>
</View>
),
})
},
},
);
this.props is only used in a react class. I assume you're using react-navigation v2 then you should dispatch DrawerAction like below
import React, { Component } from 'react';
import { createStackNavigator, NavigationActions } from 'react-navigation'
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import { DrawerActions } from 'react-navigation';
import { Draw } from './DrawNav.js'
export const RootStack = createStackNavigator (
{
DrawNav: {
screen: Draw,
navigationOptions: ({ navigation }) => ({
//Hide the shadow of the header
headerStyle: {
elevation:0,
shadowColor: 'transparent',
shadowRadius: 0,
shadowOffset: {
height: 0,
}
},
headerLeft: (
<View style={{marginLeft: 10}}>
<Icon
name="menu"
size={25}
color="#D4AF37"
onPress={() => navigation.dispatch(DrawerActions.openDrawer())}
/>
</View>
),
})
},
},
);
How can I change Background Color of the drawer fully? I don't need to change drawer items need to change the background color of the drawer fully. By default, it's white while I need to make it Green. Is there any demo example?
Old question, but this may help people that are looking for this solution. On createDrawerNavigator you have a drawerConfig property called drawerBackgroundColor.
Example:
import React from 'react';
import { createDrawerNavigator } from 'react-navigation';
import Home from '../screens/Home';
import Screen2 from '../screens/Screen2';
export default createDrawerNavigator({
Home,
Screen2
}, {
drawerPosition: 'left',
drawerBackgroundColor: '#0000FF',
});
You can read more about it, on React Navigation documentation: https://reactnavigation.org/docs/en/drawer-navigator.html
This Current example can help you , This the DrawerNavigtor using DrawerContent , Need to change the style of DrawerContent
const Main = DrawerNavigator({
home: { screen: HomePage },
}, {
drawerWidth: 250,
drawerPosition: 'right',
contentComponent: props => <DrawerContent {...props} />,
});
export default Main;
You can Change the style by using the this code below
class DrawerContent extends Component {
render() {
return (
<ScrollView style={styles.container}>
<View style={{ flex: 1 }}>
<Button transparent info onPress={() => { this.handlechange(); }}>
<Text style={{ fontSize: 16 }}>Change Email</Text>
</Button>
</View>
</ScrollView>
);
}
}
const styles = {
container: {
flex: 1,
padding: 20,
backgroundColor: 'Green',
},
};
export default DrawerContent;
This can change the background color
I am trying to customize drawer navigator in my app. I am using react-navigation. When first time I've inserted this code it showed just white screen and even navigation links were disappeared after making few things it showed this error screen.
Before that it showed just white screen inside drawer without my links. Here is the code.
App.js
import React from 'react';
import {StyleSheet, Text, View } from 'react-native';
import WelcomeScreen from './screens/WelcomeScreen';
import SigninScreen from './screens/SigninScreen';
import SignupScreen from './screens/SignupScreen';
import HomeScreen from './screens/HomeScreen';
import FoodScreen from './screens/FoodScreen';
import RestaurantsScreen from './screens/RestaurantsScreen';
import ProfileScreen from './screens/ProfileScreen';
import FavoritesScreen from './screens/FavoritesScreen';
import SettingsScreen from './screens/SettingsScreen';
import { TabNavigator, DrawerNavigator, StackNavigator,contentComponent } from 'react-navigation';
import {DrawerContent} from './components/DrawerContent'
export default class App extends React.Component {
render() {
const MainNavigator = TabNavigator({
welcome: { screen: WelcomeScreen },
signin: { screen: SigninScreen },
signup: { screen: SignupScreen },
main: {
screen: DrawerNavigator({
home: { screen: HomeScreen },
food: { screen: FoodScreen },
restaurants: { screen: RestaurantsScreen },
profile: {
screen: StackNavigator({
profilw: { screen: ProfileScreen },
settings: { screen: SettingsScreen }
})
}
},
{
contentComponent: props => <DrawerContent {...props} />,
}
)
}
},
);
return (
<MainNavigator />
);
}
}
DrawerContent.js
import React, { Component } from "react";
import { View, ScrollView,Button,Text } from "react-native";
class DrawerContent extends Component {
render() {
return (
<ScrollView style={styles.container}>
<View style={{ flex: 1 }}>
<Button transparent info onPress={() => { this.handlechange(); }}>
<Text style={{ fontSize: 16 }}>Change Email</Text>
</Button>
</View>
</ScrollView>
);
} }
const styles = {
container: {
flex: 1,
padding: 20,
backgroundColor: 'Green',
}, };
export default DrawerContent;
The button you are using is the button that should be imported from the
native base , as you are using the style property for that , or simply used this instead of that button code , replace it with below one
<Button onPress={() => { this.handlechange(); }} title="Learn More" color="#841584" />
Using this will help you with that
I think it's because contentComponent expects a class component, instead of using react-navigation, use react-navigation-drawer to import createDrawerNavigaor and DrawerNavigatorItems