react-native custom header navigator has unwanted margins - react-native

I have drawer and stack navigator. I disabled header of drawer and stack navigators. And create my own header component but I cannot rid of the edge margins of my custom header component.
[Screenshot]
https://i.stack.imgur.com/5jWv3.png
const styles = StyleSheet.create({
header: {
backgroundColor: 'purple',
width: '100%',
height: '100%',
flexDirection: 'row',
alignItems: 'center',
},
headerText: {
fontWeight: 'bold',
fontSize: 21,
letterSpacing: 1,
paddingLeft: 45,
paddingBottom: 2,
},
icon: {
fontSize: 30,
color:"white",
position: 'absolute',
zIndex: 3,
}});
 
return (
<View style={styles.header}>
<View style={styles.header}>
<MaterialIcons style={styles.icon} name="menu" onPress={openMenu}/>
<Text style={styles.headerText}>{titleName}</Text>
</View>
</View>
)

This is your header Component. Have you checked if the Main Container does not have a paddingHorizontal in the Screen?

Solution #1
Change:
return (
<View style={styles.header}>
<View style={styles.header}>
<MaterialIcons style={styles.icon} name="menu" onPress={openMenu}/>
<Text style={styles.headerText}>{titleName}</Text>
</View>
</View>
)
to:
return (
<View style={styles.header}>
<MaterialIcons style={styles.icon} name="menu" onPress={openMenu}/>
<Text style={styles.headerText}>{titleName}</Text>
</View>
)
Solution #2
import {Dimensions} from 'react-native'
let width=Dimensions.get('screen').width
let height=Dimensions.get('screen').height
then:
const styles = StyleSheet.create({
header: {
backgroundColor: 'purple',
width: width,
height: height,
flexDirection: 'row',
alignItems: 'center',
},
})

I fixed the issue with opening the app on web to see styles of parent the element. In this case my parent element is the <Stack.screen> home tab.
I just checked the styles on web which is
marginLeft: 16,
marginRight: 16,
maxWidth: 470,
and override these styles with
marginLeft: 0,
marginRight: 0,
maxWidth: '100%',
screenshot of the solution result
<Stack.screen
options={{headerBackgroundContainerStyle: {
marginLeft: 0,
marginRight: 0,
maxWidth: '100%'
}
}}
/>

Related

React Native scrollview stops scrolling after screen change

I have a ScrollView in react native where the functionality stops working only after I change the screen. For example: when I reload and land on the home screen (with the ScrollView) it all works fine. but when I use the bottom tab navigator to change the screen and return back to the home screen, it doesn't work anymore.
Thanks in advance for the help!
const Home = () => {
return (
<SafeAreaView style={{ flex: 1, backgroundColor: COLORS.background }}>
<Header screenName="home" />
<View style={{ color: COLORS.secondary, flex: 14, paddingHorizontal: SIZES.width / 20}}>
<ScrollView
showsVerticalScrollIndicator={false}
>
<Text style={styles.popularHeader}>Title</Text>
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}
style={styles.popular}
>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
</ScrollView>
<View style={styles.event}>
<Text style={styles.eventTitle}>event title</Text>
<Text style={styles.eventDate}>date</Text>
</View>
</ScrollView>
</View>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
popularHeader: {
color: COLORS.text,
fontSize: SIZES.h4,
alignSelf: 'center',
marginBottom: 10
},
popular: {
borderBottomWidth: 1,
paddingBottom: SIZES.height / 30,
marginBottom: SIZES.height / 30,
borderBottomColor: COLORS.secondary
},
popularItem: {
width: 100,
height: 100,
borderRadius: 5,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: COLORS.text,
marginRight: 10,
},
event:{
height: SIZES.height / 3,
backgroundColor: COLORS.text,
borderRadius: 5,
marginBottom: SIZES.height / 30,
padding: 10
},
eventTitle: {
fontSize: SIZES.h3,
alignSelf: 'center'
},
eventDate: {
fontSize: SIZES.h6,
alignSelf: 'center'
}
})
In case someone wonders, after a three-day-long search I found out the problem was with the bottomTabNavigator not unmounting the screens after a change of screen. so the solution that fixed my problem is:
in screenOptions:
unmountOnBlur: true

Touchables in react native

I am trying to add some space in the middle of two touchable components wrapped in a view
How do i go about doing that, I will include my style sheet
return (
<>enter code here
<View style={styles.container}>
<View style={styles.scrollContainer}>
<ScrollView
horizontal
pagingEnabled
showsHorizontalScrollIndicator={false}
>
{images.map((image) => (
<Image key={image} style={styles.image} source={image} />
))}
</ScrollView>
</View>
<View style={styles.btn}>
`enter code here`<LinearGradient
colors={["#0A5640", "#0A5640"]}
style={{ alignItems: "center", borderRadius: 10 }}
>
<TouchableOpacity onPress={() => navigation.navigate("SignIn")}>
<Text style={styles.btnAuth}>Sign In</Text>
</TouchableOpacity>
</LinearGradient>
<LinearGradient
colors={["#FFC72A", "#FFC72A"]}
style={{ alignItems: "center", borderRadius: 10 }}
>
<TouchableOpacity
onPress={() => navigation.navigate("CreateAccount")}
>
<Text style={styles.btnAuth}>Sign Up</Text>
</TouchableOpacity>
</LinearGradient>
</View>
</View>
</>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: "#0A5640",
},
scrollContainer: {
height: "100%",
},
image: {
width,
height: "100%",
},
btnAuth: {
fontSize: 18,
fontWeight: "bold",
paddingHorizontal: 60,
paddingVertical: 10,
color: "#fff",
marginLeft: 10,
},
btn: {
color: "#fff",
marginTop: 70,
flexDirection: "row",
alignItems: "center",
justifyContent: "space-around",
alignContent: "space-between",
zIndex: 200,
paddingRight: 10,
paddingLeft: 10,
bottom: 3,
position: "absolute",
},
});
Did you mean the space between SignIn and SignUp button? You can use flex or width to define the touchableOpacity width. Is this something that you tried to achieve?
import * as React from 'react';
import {Text,View,StyleSheet,TouchableOpacity,}from 'react-native';
export default function App() { return (
<View style={styles.container}>
<View style={styles.btn}>
<TouchableOpacity style={styles.btnAuth}>
<Text style={{color:'#000', fontSize:18}}>Sign In</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.btnAuth}>
<Text style={{color:'#000', fontSize:18}}>Sign Up</Text>
</TouchableOpacity>
</View>
</View> ); }
const styles = StyleSheet.create({ container: { backgroundColor: '#0A5640', flex:1 }, btnAuth: {
padding: 20,
width : '45%',
alignItems:'center',
backgroundColor:'yellow' }, btn: {
color: '#fff',
width: '100%',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-around',
bottom: 3,
position: 'absolute', }, });

How to add opacity to touchable opacity with image background in a flatlist?

I have a flatlist that renders a clickable item (with touchable opacity) and an image background with some text in the middle.
The goal is to render a flatlist that looks like this:
I am trying to add light opacity to the image by adding a view with an overlay and I tried multiple solutions but nothing seems to work.
This is what I have achieved so far:
This is the item in the flatlist:
// console.log("item", item.latest_image.media[0].url);
if (item.empty === true) {
return <View style={[styles.item, styles.itemInvisible]} />;
}
return (
<TouchableOpacity
onPress={() =>
this.props.navigation.push(Screens.Photos, {
team: item,
id: this.props.navigation.getParam("team").id
})
}
>
<ImageBackground
style={styles.backgroundImage}
imageStyle={{ borderRadius: theme.borders_MediumRadius.borderRadius, backgroundColor: 'rgba(255,0,0,.6)' }}
source={{ uri: item.latest_image.media[0].url }}
>
<Text style={styles.itemText}>{item.title.toUpperCase()}</Text>
</ImageBackground>
</TouchableOpacity>
);
};
This is the flatlist:
<SafeAreaView style={{ flex: 1 }}>
<ScrollView
style={{ flex: 1, backgroundColor: "black" }}
scrollEnabled={scrollEnabled}
onContentSizeChange={this.onContentSizeChange}
>
<FlatList
data={formatData(this.state.data, numColumns)}
style={styles.container}
renderItem={this.renderItem}
numColumns={numColumns}
scrollEnabled={scrollEnabled}
/>
<FollowUs />
</ScrollView>
</SafeAreaView>
);
and these are the style props:
container: {
margin: 7.5,
backgroundColor: "black"
},
backgroundImage: {
flex: 1,
height: hp("25"),
backgroundColor: "black",
margin: 7.5,
justifyContent: "center",
alignItems: "center"
},
itemInvisible: {
backgroundColor: "transparent"
},
itemText: {
fontFamily: "RobotoCondensed-Bold",
fontSize: RF(4),
color: "black"
},
overlay: {
backgroundColor: "rgba(255,0,0,0.5)",
alignItems: "center",
justifyContent: "center",
flex: 1,
margin: 7.5,
height: hp("25"), // approximate a square
borderRadius: theme.borders_MediumRadius.borderRadius
}
});
If anybody could help me it would be really appreciated!
Thanks
Here's an example of how you can achieve this
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<TouchableOpacity
style={{
backgroundColor: '#FFF',
height: 200,
width: 200,
position: 'relative'
}}>
<ImageBackground
source={{ uri: 'https://placehold.it/200x200' }}
style={{
height: 200,
width: 200,
opacity: 0.6,
position: 'absolute',
}}
/>
<View
style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Hello World!</Text>
</View>
</TouchableOpacity>
</View>
);
}
}
Here's an example Snack - https://snack.expo.io/#hannigan/lonely-bubblegum
You should also set the activeOpacity on the TouchableOpacity to be the same value as what you will set on the ImageBackground (0.6)
Solved it by adding a view around the text and giving it these props:
<View
style={{
flex: 1,
alignItems: "center",
justifyContent: "center",
backgroundColor: "rgba(255,255,255,0.75)",
borderRadius: theme.borders_MediumRadius.borderRadius
}}
>
<Text style={styles.itemText}>{item.title.toUpperCase()}</Text>
</View>

Custom Component not appearing

I made a custom bottom Navbar for my React-Native App which I want to stick at the bottom of the screen.
This is what it looks like
<View style={styles.NavBarBottom}>
<TouchableOpacity
onPress={() => this.activeTab("home")}>
<Text> <Icon name="bitcoin" size={30} color={ this.state.activeTab == "home" ? "#fbc02d" : "white"} /></Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => this.activeTab("News")}>
<Text> <Icons name="news" size={30} color={this.state.activeTab == "News" ? "#fbc02d" : "white"} /> </Text>
</TouchableOpacity>
</View>
where Styles are this
const styles = StyleSheet.create({
NavBarBottom : {
paddingTop: 8,
borderTopWidth: 2,
backgroundColor: "#005cb2",
display: "flex",
flexDirection: "row",
justifyContent: "space-around",
padding: 5,
}
})
Now this successfully works and show in case of Home Screen but on another screen when I import it, It appears only when the content is loading and afterwards it doesn't appear (attaching the GIF for the same in end). Also on the page where I am just passing the props, it doesn't appear
The code for other page would be
<View>
<Header
navigation = {this.props.navigation}
enable = "true" />
<ScrollView>
<View style={listOfCurrencies}>
{ CurrencyDisplay.map(data =>
<TouchableOpacity>
<View style={IndvidualCurrencyMain}>
<Text style={dataLong}> {data["long"]}</Text>
<Text style={dataShort}>{data["short"]}</Text>
</View>
</TouchableOpacity>)}
</View>
</ScrollView>
<BottomNavigation />
</View>
With Styles like this
const styles = StyleSheet.create({
listOfCurrencies: {
display: "flex",
flexDirection: "column",
marginBottom: 60
},
IndvidualCurrencyMain: {
backgroundColor: "white",
display: "flex",
flexDirection: "row",
height: 80,
alignItems: "center",
height: 50,
borderRadius: 10,
marginLeft: 3,
marginRight: 3,
marginTop: 5,
marginBottom: 5
},
dataLong:{
marginLeft: 3,
width: 150
},
dataShort: {
marginLeft: "48%",
marginRight: 5
},
index: {
marginLeft: 2,
width: 20
}
})
Any idea why it won't be appearing?
You can try this just by adding the main view flex:1 style

How to hide or remove shadow or bottomBorder in react-native IOS

I am using react-native-router-flux v4.0 library for showing navigation bar in react-native.
Here I created a custom navigation bar.
Here is my code :
_renderLeft() {
return (
<TouchableOpacity
style={{justifyContent: 'flex-start', alignItems: 'flex-start', alignSelf: 'flex-start'}}
onPress={Actions.pop}>
<Image
style={{width: 24, height: 24}}
resizeMode="contain"
source={require('../../assets/images/ico_swipe.png')}></Image>
</TouchableOpacity>
)
}
_renderMiddle() {
return (
<View style={[styles.navBarTitleView]}>
<Text style={[styles.navBarTitle]}>{this.props.title}</Text>
</View>
)
}
_renderRight() {
return (
<TouchableOpacity
style={{justifyContent: 'flex-start', alignItems: 'flex-start', alignSelf: 'flex-start'}}
onPress={Actions.pop}>
<Image
style={{width: 24, height: 24}}
resizeMode="contain"
source={require('../../assets/images/ico_home.png')}></Image>
</TouchableOpacity>
)
}
render() {
StatusBar.setBarStyle('light-content', true);
return (
<Header style={[styles.container]}>
<Left style={{flex: 1}}>
{this._renderLeft()}
</Left>
<Body style={{flex: 3}}>
<Title style={styles.navBarTitle}>{this.props.title}</Title>
</Body>
<Right style={{flex: 1}}>
{this._renderRight()}
</Right>
</Header>
)
}
Here is my Style:
const styles = StyleSheet.create({
container: {
backgroundColor: AppColors.colorToolBar,
elevation:0
},
navBarTitleView: {
flex: 2,
},
navBarTitle: {
fontSize: 20,
fontFamily: AppStyles.fontFamilyMedium,
color: AppColors.white,
alignSelf: 'center'
},
menuItem:{
flex: 1, flexDirection: 'row', padding: 20
},
menuTitle:{flex: 20, justifyContent: 'flex-start', alignItems: 'center',
alignSelf: 'flex-start'},
menuNextArrow:{flex: 1}
});
Here I used it :
<Stack key="Key" hideTabBar>
<Scene key="Key"
navBar={MyCustomNavBarLocation}
component={myFileLocation}
title="Round 1"
onLeft={Actions.pop}
BackHandler={Actions.pop}
back
/>
</Stack>
I am getting it proper in Android like:
But in Iphone its not coming proper:
Here If u see the second Image u saw one grey Line between navigation bar and TimeRemaining view I want to remove that.
Thanks
The issue is in Header component of NativeBase
That bottom border line is coming from the Header style. So according to the issue raised here, use,
<Header noShadow={true} hasTabs={true}
for resolving this issue.
Its late but more accurate answer,
Add this line in your router to manage shadow:
<Router
navigationBarStyle={{ ...Platform.select({
ios: {
// marginTop: StatusBar.currentHeight
elevation: 0,
shadowOpacity: 0,
borderBottomWidth: 0,
}
})
}}
>