Aligning image over 2 background views - reactjs

I have two background views with different colors. Both their own size.
Over these backgrounds, I need an image aligned on the left, over both other views.
how can i accomplish this?
export default class MainScreen extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.rectangle1}>
<Image
source={require('../assets/dame.png')}
style = {styles.image}/>
</View>
<View style={styles.rectangle2}>
</View>
</View>
)
}
}
Added sample image what to accomplish:

besides the use of absolute positioning, here is another tip to achieve a similar layout on most devices is the use of Dimensions and constants for the width and height of the devices that.
based on the information provided, here is how my design will look like:
////import { Dimensions } from react-native;
const { width: deviceWidth, height: deviceHeight } = Dimensions.get('window');
export default class MainScreen extends Component {
render() {
return (
<View style={{ backgroundColor: 'magenta', height: deviceHeight }}>
<View
style={{
width: deviceWidth * 0.44,
alignSelf: 'flex-start',
}}
>
<Image
source={{ uri: 'https://ui-avatars.com/api/?name=Alice' }}
style={{ width: '100%', height: '100%' }}
/>
</View>
<View
style={{
backgroundColor: 'black',
position: 'absolute',
width: deviceWidth * 0.56,
height: deviceWidth * 0.56,
alignSelf: 'flex-end',
opacity: 0.7
}}
/>
<View
style={{
backgroundColor: 'yellow',
width: deviceWidth * 0.7,
height: deviceWidth * 0.1,
position: 'absolute',
top: deviceHeight * 0.8,
alignSelf: 'center',
opacity: 0.8,
}}
/>
<View
style={{
backgroundColor: 'blue',
width: deviceWidth,
height: deviceWidth * 0.14,
position: 'absolute',
bottom: 0,
alignSelf: 'flex-end',
opacity: 0.8,
}}
/>
</View>
)
}
}
i suggest you keep experimenting to find out which method works best for your application.

Related

React Native get view height

Green: full screen view
Red: is a view style absolute bottom:0
Blue: is a view style absolute height:‘100%’
WHAT I WANT
I want find the way to get height of Red View (height isn’t determined).
This height I want to use it for marginBottom for Blue View.
EXPO SNACK
<View style={{
backgroundColor: 'green',
flex:1
}}>
<View style={{
backgroundColor: 'blue',
position:'absolute',
height:'100%',
left:0,
right:0,
top:0,
marginBottom: HEIGHT_OF_RED_VIEW
}}>
</View>
<View style={{
backgroundColor: 'red',
position:'absolute',
left:0,
right:0,
bottom:0,
padding:20
}}>
<Text>Lorem Ipsum</Text>
</View>
</View>
You can try onLayout method of view it will call once the rendeing of your view is completed take a look at example below
<View
onLayout={({ nativeEvent }) => {
const { x, y, width, height } = nativeEvent.layout
setHeight(height)
}}
</View>
you can have height variable defined in your state, and you can use that variable for allocating the margin bottom
You can try code below I have made changes in your expo slack
import { Text, View} from 'react-native';
import {useState} from 'react'
export default function App() {
const [height, setHeight] = useState('')
return (
<View style={{
backgroundColor: 'green',
flex: 1
}}>
<View style={{
backgroundColor: 'blue',
position: 'absolute',
// height: '100%',
left: 0,
right: 0,
top: 0,
bottom: height,// here is you want to have some addintional spacing try adding some value in you hight (ex. height+20)
}}>
</View>
<View
onLayout={({ nativeEvent }) => {
const { x, y, width, height } = nativeEvent.layout
setHeight(height)
}}
style={{
backgroundColor: 'red',
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
padding: 20
}}>
<Text>Lorem Ipsum</Text>
</View>
</View>
);
}

Image background which stick to top React Native

I want to achieve the following view, where i have a blue image which is added as a background sticking to the top of the container. Any idea on how this can be achieved in React Native?
P.S. I am using Native Base too in the project
import * as React from 'react';
import { View, Image, Dimensions } from 'react-native';
const windowWidth = Dimensions.get('window').width;
const windowHeight = Dimensions.get('window').height;
export default function App() {
return (
<View
style={{
flex: 1,
backgroundColor: '#ecf0f1',
}}>
<Image
resizeMode={'contain'}
source={{
uri:
'https://loremflickr.com/cache/resized/65535_51113407033_7bed78119e_z_640_360_nofilter.jpg',
}}
style={{ width: windowWidth, height: 300, position: 'absolute' }}
/>
<View style={{ flex: 1, justifyContent: 'flex-end', paddingBottom: 10 }}>
<View
style={{
width: windowWidth * 0.8,
height: 350,
backgroundColor: 'white',
alignSelf: 'center',
}}
/>
</View>
</View>
);
}

How to make center of barcode scanner transparent?

I am creating a barcode scanner using expo-barcode-scanner
I have created the below screen the whole screen is transparent
I am trying to create the below screen only the QR code square is transparent
<BarCodeScanner
style={styles.mainContainer}>
<TouchableOpacity
onPress={() => {
navigation.navigate('Home')
}}
style={styles.backButton}
>
<Entypo
style={{ color: Colors.SAWhite }}
name="chevron-thin-left"
size={25}
/>
</TouchableOpacity>
<View style={styles.qrContainer}></View>
<View style={styles.messageBox}>
<FontAwesomeIcon
name="qrcode"
size={50}
style={isValid ? styles.initialColor : styles.errorColor}
/>
{isValid ? (
<Text style={[fonts.SemiBoldTitle, styles.initialColor]}>
{Languages.Scanner.initialMessage}
</Text>
) : (
<Text style={[fonts.SemiBoldTitle, styles.errorColor]}>
{Languages.Scanner.errorMessage}
</Text>
)}
</View>
</BarCodeScanner>
styles
const styles = StyleSheet.create({
mainContainer: {
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
overflow: 'hidden',
},
backButton: {
alignSelf: 'flex-start',
marginLeft: '5%',
},
qrContainer: {
width: 220,
height: 220,
borderColor: Colors.SAWhite, //white
borderWidth: 1,
margin: '10%',
},
messageBox: {
width: '85%',
height: '30%',
backgroundColor: Colors.SAWhite,
borderColor: Colors.SABlack,
borderWidth: 1,
borderRadius: 10,
alignItems: 'center',
justifyContent: 'space-evenly',
},
initialColor: {
color: Colors.SASecondary,//grey
textAlign: 'center',
marginLeft: '10%',
marginRight: '10%',
},
errorColor: {
color: Colors.SARed,
textAlign: 'center',
marginLeft: '10%',
marginRight: '10%',
},
})
I have tried to wrap it around a view but it also makes the center box as coloured
i have tried expo's barcode code but it is also not good implementation
You can build up the overlay around the transparent part using multiple absolute positioned views, leaving the center empty.
const scanOverlay = {
position: 'absolute',
backgroundColor: 'rgba(255,0,0,0.5)',
};
<View>
<Camera />
<View style={StyleSheet.absoluteFill}>
<View style={[scanOverlay, {top: 0, left: 0, width: '25%', bottom: 0}]} />
<View style={[scanOverlay, {top: 0, left: '25%', right: '25%', height: '25%'}]} />
<View style={[scanOverlay, {bottom: 0, left: '25%', right: '25%', height: '25%'}]} />
<View style={[scanOverlay, {top: 0, right: 0, width: '25%', bottom: 0}]} />
</View>
</View>;

React Native Position Absolute Button doesn't work

clicking this position absolute button is not working when I touch it.
code
render() {
return (
<Container>
<View style={{ borderColor: '#fff',height:'30%'}}>
<ImageBackground
source={{uri: 'https://i.ibb.co/TwdZhVM/image-5.png'}}
style={styles.singleCategoryViewBackgroundImage} >
</ImageBackground>
<View style={styles.overlay}>
<Thumbnail large
style={styles.profileImage}
source={{uri:"https://firebasestorage.googleapis.com/v0/b/weddi-4c344.appspot.com/o/Laser%20%26%20Beauty%20Centers%2FDivine%20beauty%20clinics%2FLogo.jpg?alt=media"}} />
<View style={styles.titleContent}>
<Text style={styles.titleText}> Divine Beauty Center</Text>
<Text note>Cairo - Naser City</Text>
<View style={{flexDirection: 'row'}}>
<Button rounded style={styles.actionButton}
onPress={() =>
ActionSheet.show(
{
options: BUTTONS,
cancelButtonIndex: CANCEL_INDEX,
destructiveButtonIndex: DESTRUCTIVE_INDEX,
title: "Testing ActionSheet"
},
buttonIndex => {
this.setState({ clicked: BUTTONS[buttonIndex] });
}
)}
>
<Icon ios="ios-pin" android="md-pin" color="#1a1917"/>
</Button>
<Button rounded style={styles.actionButton}>
<Icon ios="ios-call" android="md-call" color="#1a1917"/>
</Button>
</View>
</View>
</View>
</View>
<Content style={{paddingHorizontal: '6%', marginTop: '20%', height: '20%'}}>
</Content>
</Container>
styles
overlay: {
alignItems: 'center',
height: '65%',
backgroundColor: colors.offWhite,
position: 'absolute',
top: '68%',
left: 0,
right: 0,
bottom: 0,
zIndex: 1,
marginHorizontal: '6%',
borderRadius: 10/2,
opacity: 0.95,
},
singleCategoryViewBackgroundImage: {
width: '100%',
height: '100%',
justifyContent:'center',
alignContent:'center',
},
profileImage: {
position: 'absolute',
top: '-38%',
left: '36%',
right: 0,
bottom: 0,
zIndex: 2,
alignSelf: 'center',
overflow: 'hidden',
borderWidth: 2,
borderColor: '#b42334',
width: 100,
height: 100,
borderRadius: Platform.OS === 'ios' ? 0 : 150/2,
},
titleContent: {
zIndex: 40,
marginVertical: '15%',
alignItems: 'center',
},
titleText: {
fontFamily: 'Roboto',
fontWeight: 'bold',
},
actionButton: {
width:50,
zIndex: 50,
margin: '2%',
backgroundColor: colors.red,
}
I tried replacing the native-base button with react native button still doesn't work but when I make the button outside this position absolute mess it works great but I like how it looks inside this view so any ideas how can I get it to work that way?
From https://www.w3schools.com/cssref/pr_pos_z-index.asp: "Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky)." Since actionButton doesn't specify a position attribute, it's position is the default position value (static) and z-index does not apply. So your absolutely positioned Views are on top of your button.

Building Image Card in React Native

I'm trying to build a simple image card component in React Native and got some problems. This is my component now (It's available for you on snack):
I can't find a way to set border only on the top of the image on the card, keeping the bottom border flat.
Desired form:
The Image component doesn't seen to be rendered from the top showing the model's face, instead it's getting centered showing her body.
Here it's the original image for comparison:
Use this code. Added overflow: "hidden" to the View and removed borderRadius for Image. Tested in IOS.
import * as React from 'react';
import { Text, View, Image } from "react-native";
export default class RootComponent extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<View style={{ backgroundColor: "#eee", borderRadius: 10, overflow: "hidden" }}>
<View>
<Image
source={require("./assets/h4.jpg")}
style={{
height: 135,
width: 155
}}
/>
</View>
<View style={{ padding: 10, width: 155 }}>
<Text>Title</Text>
<Text style={{ color: "#777", paddingTop: 5 }}>
Description of the image
</Text>
</View>
</View>
</View>
);
}
}
By removing the height from the <Image> and setting it in its parent view, the image will be shown from the top.
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<View style={{ backgroundColor: "#eee", borderRadius: 10, overflow: 'hidden' }}>
<View style={{ height: 135, width: 155, overflow: 'hidden' }}>
<Image
source={require("./assets/h4.jpg")}
style={{
width: 155
}}
/>
</View>
<View style={{ padding: 10, width: 155 }}>
<Text>Title</Text>
<Text style={{ color: "#777", paddingTop: 5 }}>
Description of the image
</Text>
</View>
</View>
</View>
You can directly do it with borderTopLeftRadius and borderTopRightRadius in a Card.
<Card
containerStyle={styles.boxCon}
featuredTitle={title}
image={{
uri: urlToImage
}}
imageStyle={{ borderTopLeftRadius: 10, borderTopRightRadius: 10 }}
>
const styles = {
boxCon: {
margin: 15,
marginHorizontal: 10,
marginBottom: 17.5,
borderColor: '#FFFFFF',
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.3,
shadowRadius: 5,
elevation: 5,
borderRadius: 10
}
};

Resources