Make Image Fit Inside a View React native - react-native

there's a gap above the Image inside the View. Im trying to make it fit the Image inside my View
My Output
What i want to achieve
Here is my code
import React from 'react';
import {View, Text, StyleSheet, Image, TouchableOpacity} from 'react-native';
import THEME from '../../constants/theme.constants';
import SmallButton from './SmallButton';
import default_pp from '../../assets/default_pp.png';
import thumbnail_post from '../../assets/thumbnail_post.png';
function PostServiceProvider({user, ratings, rate, description}) {
return (
<View style={styles.container}>
<View style={styles.headerPost}>
<Image source={default_pp} style={styles.profilePicContainer} />
<TouchableOpacity style={{marginLeft: 10}}>
<Text style={styles.priceText}>{user}</Text>
</TouchableOpacity>
<View style={{position: 'absolute', right: 5}}>
<Text>{ratings}</Text>
</View>
</View>
<View style={styles.cardContainer}>
<Image source={thumbnail_post} style={styles.thumbnailImg} />
<View style={styles.rateContainer}>
<Text style={styles.text}>Daily Rate: </Text>
<Text style={styles.priceText}>Php 400</Text>
<View style={styles.buttonContainer}>
<SmallButton label="Hire" />
</View>
</View>
<View style={styles.br} />
<View style={{padding: 20}}>
<Text numberOfLines={2} style={styles.text}>
{description}
</Text>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
width: '100%',
borderRadius: 50,
borderColor: 'transparent',
marginTop: 20,
},
headerPost: {flexDirection: 'row', alignItems: 'center'},
profilePicContainer: {
height: 40,
width: 40,
borderRadius: 50,
borderWidth: 1,
},
cardContainer: {
width: '100%',
flex: 1,
borderColor: 'rgba(0,0,0,0.2)',
borderRadius: 10,
borderWidth: 0.5,
marginTop: 10,
alignItems: 'center',
},
thumbnailImg: {width: '100%', height: 300},
rateContainer: {
flexDirection: 'row',
alignItems: 'center',
width: '100%',
padding: 20,
},
buttonContainer: {width: '22%', position: 'absolute', right: 15},
br: {height: 1, width: '90%', backgroundColor: 'rgba(0,0,0,0.1)'},
text: {color: THEME.FONT_FADE},
priceText: {fontWeight: 'bold', fontSize: 17},
});
export default PostServiceProvider;

Add resizeMode="cover" props in your image component like this :
... your previous code
<View style={styles.cardContainer}>
<Image source={thumbnail_post} style={styles.thumbnailImg} resizeMode="cover" />
... your rest of code

Related

Place Item to Bottom React Native

I'm creating a react native app i have a item - a plus button(shown in image)
I want this to the bottom of my page onto footer -
but the problem is that when i place this button on bottom with position: 'absolute', bottom: 0 it's only going to the bottom of the hero section & not to the footer
Code -
<View style={styles.main}>
<View style={styles.hero}>
<View style={styles.flex}>
<View style={styles.container}>
</View>
</View>
<View style={styles.flex2}>
</TouchableOpacity>
</View>
<View style={styles.container12}>
</View>
</View>
</View>
<TouchableOpacity>
<View style={styles.container33}>
<EntypoIcon name="plus" style={styles.icon28}></EntypoIcon>
</View>
</TouchableOpacity>
Main Styles -
main: {
width: '100%',
height: '100%',
},
hero: {
alignSelf: 'stretch',
height: 250,
backgroundColor: '#fff',
borderBottomLeftRadius: 50,
borderBottomRightRadius: 50,
},
container33: {
width: 50,
height: 50,
backgroundColor: '#1DA6FA',
// flex: 1,
borderRadius: 50,
position: 'absolute',
bottom: 0,
right: 10,
alignItems: 'center',
justifyContent: 'center'
},
The problem si that you are applying the absolute css position to the View inside the TouchableOpacity and not to the TouchableOpacity itself.
To achieve what you want to do, you should write something like:
<View style={styles.absoluteOpacity}>
<TouchableOpacity>
<View style={styles.container33}>
<EntypoIcon name="plus" style={styles.icon28}></EntypoIcon>
</View>
</TouchableOpacity>
</View>
And then the css:
container33: {
width: 50,
height: 50,
backgroundColor: '#1DA6FA',
// flex: 1,
borderRadius: 50,
right: 10,
alignItems: 'center',
justifyContent: 'center'
},
absoluteOpacity: {
position: 'absolute',
bottom: 0,
}

How can i set tag styles in react native?

This is my screen I want to show these tags in the next line but failed it moves into next line how can I set tag styles that can come under category types.
I want my category types like this
{item.category &&
item.category.map((item, index) => (
<View
style={{
padding: 5,
width: '30%',
backgroundColor: 'skyblue',
}}>
<Text
numberOfLines={2}
ellipsizeMode="tail"
style={{
fontSize: 16,
width: '60%',
padding: 5,
color: 'grey',
// marginLeft: 5,
borderRadius: 40,
// alignSelf: 'center',
backgroundColor: theme.colors.secondary,
textAlign: 'center',
fontFamily: Fonts.FontAwesome,
}}>
{item}
</Text>
</View>
))}
Here is your solution:
import React from "react";
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from "react-native";
const staticData = ["iOS", "Android", "Kotlin", "Boom", "Mobile", "Dev"];
const App = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={{ flex: 1 }}>
<View
style={{
flex: 1,
height: 300,
flexWrap: "wrap",
flexDirection: "row",
justifyContent: "center",
alignItems: "flex-start",
}}
>
{staticData.map((item) => {
return (
<View
style={{
width: 100,
margin: 12,
padding: 8,
borderRadius: 20,
alignItems: "center",
justifyContent: "center",
backgroundColor: "#6daf6e",
}}
>
<Text
style={{ color: "#fdfdfd", flexShrink: 1, flexWrap: "wrap" }}
>
{item}
</Text>
</View>
);
})}
</View>
</SafeAreaView>
</>
);
};
export default App;
Actually all you need to do is a main container for your tags and set the container style as:
flexWrap: "wrap",
flexDirection: "row",
justifyContent: "center",
alignItems: "flex-start",
Also, the working example repository is here as well:
https://github.com/WrathChaos/react-native-tag-wrap-overlaps-problem-solution

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 design full Center Cut FAB in react native navigation

I am need to create a custom bottom navigation with floating action button as a full center cut in react native. I will attach the image. I need exactly this and there is not tutorial to follow as at now.I need help. You can drops links I can follow.If you have done something like this,you can share the code
I did like this in my one project. create "BottomNavigator" component and import it at any class that I wish:
import React, { Component } from 'react';
import { View } from 'react-native';
import { Icon, Button } from 'react-native-elements'
class BottomNavigator extends Component {
render() {
return (
<View style={{
flex: 1,
flexDirection: 'column',
backgroundColor: '#fff'
}}>
<View style={{ position: 'absolute', padding: 5, alignSelf: 'center', backgroundColor: '#fff', width: 70, height: 70, borderRadius: 35, bottom: 25, zIndex: 5 }}>
<Button
icon={{
name: 'plus',
type: 'feather',
color: '#fff',
style: { marginRight: 0 }
}}
onPress={() => this.doSomething()}
buttonStyle={{ backgroundColor: '#000', width: 60, height: 60, borderRadius: 30 }}
containerViewStyle={{ alignSelf: 'center' }}
/>
</View>
<View style={{ position: 'absolute', backgroundColor: '#3F51B5', bottom: 0, zIndex: 1, width: '100%', height: 60, flexDirection: 'row', justifyContent: 'space-between', paddingHorizontal: 15, paddingVertical: 10 }}>
<Icon
name='list'
type='feather'
color='#fff'
onPress={() => this.doSomething()} // Ex : openDrawer() in react-navigation
/>
<View style={{ flexDirection: 'row', justifyContent: 'center' }}>
<Icon
name='heart'
type='feather'
color='#fff'
containerStyle={{ marginHorizontal: 10 }}
/>
<Icon
name='search'
type='feather'
color='#fff'
/>
</View>
</View>
</View>
);
}
}
export default BottomNavigator;
And in any class import and use <BottomNavigator />.
I am using react-native-elements and vector-icons. It's not necessary just recommended.
Used inline style for your easy edit. I hope this helps you.

In React Native, how do I put a view on top of another view, with part of it lying outside the bounds of the view behind?

I'm trying to make a layout as per below with React Native.
How do I specify the position of B relative to A?
With iOS Interface Builder and autoconstraints, this can very explicitly be done and is a breeze. It's not so obvious how one might achieve this with React Native.
Add the following style to the "floating" view:
position: 'absolute'
You may also need to add a top and left value for positioning.
The above solutions were not working for me. I solved it by creating a View with the same background colour as the parent and added negative margin to move the image upwards.
<ScrollView style={{ backgroundColor: 'blue' }}>
<View
style={{
width: '95%',
paddingLeft: '5%',
marginTop: 80,
height: 800,
}}>
<View style={{ backgroundColor: 'white' }}>
<Thumbnail square large source={{uri: uri}} style={{ marginTop: -30 }}/>
<Text>Some Text</Text>
</View>
</View>
</ScrollView>
and I got the following result.
You can use zIndex for placing a view on top of another. It works like the CSS z-index property - components with a larger zIndex will render on top.
You can refer: Layout Props
Snippet:
<ScrollView>
<StatusBar backgroundColor="black" barStyle="light-content" />
<Image style={styles.headerImage} source={{ uri: "http://www.artwallpaperhi.com/thumbnails/detail/20140814/cityscapes%20buildings%20hong%20kong_www.artwallpaperhi.com_18.jpg" }}>
<View style={styles.back}>
<TouchableOpacity>
<Icons name="arrow-back" size={25} color="#ffffff" />
</TouchableOpacity>
</View>
<Image style={styles.subHeaderImage} borderRadius={55} source={{ uri: "https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Albert_Einstein_1947.jpg/220px-Albert_Einstein_1947.jpg" }} />
</Image>
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "white"
},
headerImage: {
height: height(150),
width: deviceWidth
},
subHeaderImage: {
height: 110,
width: 110,
marginTop: height(35),
marginLeft: width(25),
borderColor: "white",
borderWidth: 2,
zIndex: 5
},
You can use this OverlayContainer. The trick is to use absolute with 100% size. Check below an example:
// #flow
import React from 'react'
import { View, StyleSheet } from 'react-native'
type Props = {
behind: React.Component,
front: React.Component,
under: React.Component
}
// Show something on top of other
export default class OverlayContainer extends React.Component<Props> {
render() {
const { behind, front, under } = this.props
return (
<View style={styles.container}>
<View style={styles.center}>
<View style={styles.behind}>
{behind}
</View>
{front}
</View>
{under}
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
height: '100%',
justifyContent: 'center',
},
center: {
width: '100%',
height: '100%',
alignItems: 'center',
justifyContent: 'center',
},
behind: {
alignItems: 'center',
justifyContent: 'center',
position: 'absolute',
left: 0,
top: 0,
width: '100%',
height: '100%'
}
})
import React, {Component} from 'react';
import {StyleSheet, View} from 'react-native';
export default class App extends Component {
render() {
return (
<View>// you need to wrap the two Views an another View
<View style={styles.box1}></View>
<View style={styles.box2}></View>
</View>
);
}
}
const styles = StyleSheet.create({
box1:{
height:100,
width:100,
backgroundColor:'red'
},
box2:{
height:100,
width:100,
backgroundColor:'green',
position: 'absolute',
top:10,
left:30
},
});
You can use react-native-view-overflow plugin for placing a view on top of another. It works like the CSS z-index property.
import ViewOverflow from 'react-native-view-overflow';
<ViewOverflow />
<View style={[styles2.cardBox, { marginTop: 50 }]}>
<View style={[styles2.cardItem]} >
<Text style={styles2.cardHeader}>userList</Text>
</View>
<View style={[styles2.cardContent]}>
<Text style={styles2.text}>overflow: "visible"</Text>
</View>
<View style={[styles2.cardItemFooter]} >
<Text style={styles2.cardTextFooter}>...</Text>
</View>
</View>
</ViewOverflow>
const styles2 = StyleSheet.create({
cardBox: {
borderLeftWidth: 0,
borderTopWidth: 0,
backgroundColor: "transparent",
borderWidth: 1,
borderColor: "#d0d0d0",
width: '94%',
alignSelf: 'center',
height: 200,
position: "relative",
borderRadius: 15,
overflow: "visible" // doesn't do anything
},
cardContent: {
textAlign: "right",
backgroundColor: "transparent",
marginTop: 15,
alignSelf: 'flex-end',
padding: 5,
},
cardHeader: {
color: '#fff',
fontFamily: 'Vazir',
fontSize: 12
},
cardItem: {
backgroundColor: "#3c4252",
borderRadius: 3,
position: "absolute",
top: -10,
right: -5,
width: 50,
height: 20,
paddingRight: 5,
},
})
The easiest way to achieve this is with a negative margin.
const deviceWidth = RN.Dimensions.get('window').width
a: {
alignItems: 'center',
backgroundColor: 'blue',
width: deviceWidth,
},
b: {
marginTop: -16,
marginStart: 20,
},
You can use elevation property for Android if you don't mind the shadow.
{
elevation:1
}
Try this:
style = {{position: 'absolute', bottom: 20, left: 20, elevation: 100}}
Based on the example above i've created a component which stacks all childeren on top of each other. You could even nest OverlayContainers inside OverlayContainers.
Usage:
<OverlayContainer>
<View style={{backgroundColor:'red', width:150, height: 150}}></View>
<View style={{backgroundColor:'yellow', width:50, height: 50}}></View>
<Text>Just some text</Text>
</OverlayContainer>
Output:
import React, { FC, PropsWithChildren } from 'react'
import { StyleSheet, View } from 'react-native'
export const OverlayContainer: FC<PropsWithChildren<unknown>> = (props) => {
return (
<View style={styles.container}>
{props.children.map((child, index) => (
<View style={styles.child} key={index}>
{child}
</View>
))}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
height: '100%',
},
child: {
position: 'absolute',
left: 0,
top: 0,
width: '100%',
height: '100%'
}
})
<SafeAreaView style={{ flex: 1 }} >
<View style={{ height: Dimensions.get('window').height / 2, backgroundColor: 'blue', justifyContent: 'center' }}>
<Text style={{ fontSize: 25, alignSelf: 'center' }} >A</Text>
<View style={{ justifyContent: 'center', height: 100, width: 100, backgroundColor: 'yellow', position: 'absolute', left: 20, top: Dimensions.get('window').height / 2 - 70 }}>
<Text style={{ fontSize: 22, alignSelf: 'center' }} >B</Text>
</View>
</View>
</SafeAreaView>