Images disappearing when using Flex - react-native

Simple issue: Whenever I use flex on an image it simply doesn't show up when I preview my app on expo.
This is the way I'm doing it:
<Image
style = {{flex: 1, height: undefined, width: undefined}}
source = {require('./img/anyImage.png')}
/>
If I just define the height and width, no flex, they show up. Is there any particular reason as to why my images aren't showing up?
Note: I've tried giving set values to both height and width.
Thanks a lot!

passing null instead of undefined should do the trick for you.
<Image
style = {{flex: 1, height: null, width: null}}
source = {require('./img/anyImage.png')}
/>
According to your code you can use one of these two options if you want to use flex.
1.giving the flex:1 to the image with null width for taking the whole view
2.use flex with resizeMode for taking the whole view
{/* THIS IS THE FIRST OPTION YOU CAN USE */}
<View>
<View style={{ width: 100, height: 100 }}>
<Image
style={{ flex: 1, width: null }}
source={require("./img/cervecerias.png")}
/>
</View>
</View>
{/* THIS IS THE SECOND OPTION YOU CAN USE */}
<View style={{ width: 200, height: 100 }}>
<Image
style={{ flex: 1 }}
source={require("./img/Cocktails.png")}
resizeMode="contain"
/>
<Text style={styles.categoriesText}> Cocktails</Text>
</View>

Related

How can I move the component which is based on below flatList to the bottom of the screen?

Everybody.
I am going to make one screen (top: flatList, bottom: some component), but the component should be posited at the bottom of the screen.
I tried several times like this, but I could not do that.
I tired:
bottom component style:
position: 'absolute',
bottom: 0,
padding: 20,
But not working for me and this component just hidden.
I am not sure why this happened.
Please help me...
Thanks
You can do something like this. The flatlist would be scrollable, you can place the view above or below based on your requirement.
//data=['aa','bb','cc']
<View style={{ flex: 1 }}>
<View style={{ backgroundColor: 'blue', height: 100, width: '100%' }} />
<FlatList
style={{ flexGrow: 0 }}
renderItem={({ item }) => (
<Text style={{ height: 100 }}>{item}</Text>
)}
data={data}
/>
<View
style={{
backgroundColor: 'red',
height: 100,
width: '100%',
marginTop: 'auto',
}}
/>
</View>
Explanation
flexGrow: 0 will make sure that the flatlist will use only available space otherwise it will grow and take the full screen.
marginTop: 'auto' will move the view to the bottom part this works for marginLeft and marginRight as well which can help when moving item to corners.
So the view at the bottom would take height:100 and full width and move to the bottom and flatlist would take the rest.
Output will be like below
Do you mean something like that?
<View style={{ flex: 1 }}>
<FlatList
style={{ flex: 1, .... }}
...
/>
<SomeComponent style={{height:80, width:'100%'}}>
...
</SomeComponent>
</View>

React native how to make an image take up full screen?

I have a component that when called should display an image for the whole screen of the phone. The issue is I cannot get the image centered. It is mostly off screen to the right with some barely visible. If anyone knows what I am doing wrong your help would be much appreciated. I am using react-native-image-zoom so the image can be zoomed on and swiped down to go away.
My image component:
const FullPicture = (props) => {
return (
<View style={{ height: '100%', width: '100%' }}>
<ImageZoom
cropWidth={Dimensions.get('window').width}
cropHeight={Dimensions.get('window').height}
enableSwipeDown
onSwipeDown={() => props.closeImage()}
>
<Image
style={{ height: props.picture.height, width: props.picture.width }}
resizeMode={'cover'}
source={{ uri: props.picture.uri }}
/>
</ImageZoom>
</View>
);
};
You need to use a Flexbox. Try to add flex: 1 to your Image's container:
<View style={{ flex: 1, height: '100%', width: '100%' }}>
<ImageZoom> ... </ImageZoom>
</View>
Try these:
set flex: 1 in your container view
use justifyContent: 'center'
check this out.
I think this is the thing what you are looking for https://youtu.be/YRrji3BmHY4
you should use ImageBackground from react-native instead of react-native-image-zoom.
when you use ImageBackground don't forget resizeMode='contain' and styling the image, the image will not get off by any side.

Responsive height of react-native-modal according to number of elements in it

i am trying to create a modal in react native application, and there is a button or touchable opacity on a screen which toggles the modal, i am trying to populate a list of items on modal by the use of mapping. But it is not working as expected because elements i create are hidden behind other elements.
I have tried using Modal from react native but i was unable to set custom size of the modal as i needed some portion of it to be transparent. That is why i moved to react-native-modal library but i am facing above mentioned error in that.
<Modal isVisible={this.state.modalVisible} >
<View style={{backgroundColor:'white', alignItems:'center', borderRadius:5}}>
<Image source={require('../../assets/Group3Copy.png')} style={{marginTop:responsiveFontSize, marginBottom:responsiveFontSize-2}}/>
<Text style={{color:'#92c848', alignSelf:'center', fontWeight:'bold', fontSize:responsiveFontSize+10}}>Well Done!</Text>
<Text style={{color:blueColor, fontWeight:'bold'}}>{this.state.plantCount} plants have been planted</Text>
<Text style={{color:blueColor, fontWeight:'bold'}}> {this.state.plantCount} پودے لگاۓ گۓ ہيں </Text>
{/* Mapping=========================================== */}
<View style={AppStyles.mainNoRow}>
<View style={AppStyles.colNoIco}>
<Image source={require('../../assets/plant.png')}/>
</View>
<View style={AppStyles.colOrderNoDetail}>
<Text style={{color:"#006f91", alignSelf:'flex-start', fontWeight:'bold',marginTop:10}}>Plant</Text>
<Text style={{color:"#747474", alignSelf:'flex-start', fontWeight:'bold',}}>Id</Text>
</View>
<View style={AppStyles.colOrderPickupYesDetail}>
<Text style={{color:greenColor ,fontWeight:'bold',}}>1 Jan 19</Text>
<Image source={require('../../assets/address.png')}/>
</View>
</View>
<TouchableOpacity onPress={this._toggleModal}
style={{
width: "90%",
height: 60,
justifyContent: "center",
alignSelf: "center",
borderWidth: 1,
borderRadius: 8,
borderColor: blueColor,
shadowColor: blueColor,
shadowOffset: { width: 0, height: 1 },
shadowOpacity: 0.9,
shadowRadius: 1,
backgroundColor: blueColor,
marginBottom:responsiveFontSize}} >
<Text style={AppStyles.textStylePlant}>OK</Text>
</TouchableOpacity>
</View>
</Modal>
I expect it's height to be set according to the number of elements mapped on it. and adjust it automatically if that is possible.
But right now elements are overlapping like shown here in this picture. https://i.imgur.com/jH9sBxV.png
Elements are hidden behind 'OK' button.
Adding marginBottom to the styling of main row seems to resolve the issue. And it also resize the height of modal accordingly.

How to add "edit" icon as a button to <Image> component in react-native

I wanna add an "Edit" button with an icon in my <Image> component. following is my code for the <View> that contain the image.
<View style={{ flex: 1 }}>
<View
style={{
justifyContent: 'space-evenly',
alignItems: 'center',
flexDirection: 'row',
paddingVertical: 10
}}
>
<Image
source={{ uri: 'https://api.adorable.io/avatars/285/test#user.i.png' }}
style={{
marginLeft: 10, width: 100, height: 100, borderRadius: 50
}}
/>
</View>
</View>
it is much better if i can do this, without replacing <ListItem> with <Image>
You can try it like:
<View>
<Image
source={{ uri: 'https://api.adorable.io/avatars/285/test#user.i.png' }}
style={{
marginLeft: 10, width: 100, height: 100, borderRadius: 50
}}
/>
<Icon name={'edit'} containerStyle={styles.icon} onPress={console.log('I was clicked')}/>
</View>
Then the icon style like below: Note the absolute position attribute
icon: {
backgroundColor: '#ccc',
position: 'absolute',
right: 0,
bottom: 0
}
Tested with Icon from react-native-elements but I guess it should work with any other Icon.
Try to use ImageBackground to wrap your icon inside it and for icon purpose use react-native-vector-icon library.
<ImageBackground source={...} style={{width: '100%', height: '100%'}}>
<Icon
name="edit"
size={18}
onPress={this.edit}
>
Edit
</Icon>
</ImageBackground>
You can use react-native-vector-icons to add an icon within the view which contains image. You can also add an icon button component using that library which will look something like this
<Icon.Button
name="edit"
backgroundColor="#3b5998"
onPress={this.edit}
>
Edit
</Icon.Button>

Vertically anchor an image to its top in React Native

I have some code to have an image of random size spread to the full width of a container + vertically center inside the container. But what I need is to anchor it at the top.
In CSS i would use background-position, but it's not supported in React Native. I feel I've tried every combination of resizeMode, absolute positioning, etc - but still haven't been able to figure it out.
<View style={{
flex: 1,
height: 120,
}}>
<Image source={{uri: source}} style={{
flex: 1,
width: '100%',
resizeMode: 'cover'
}}>
</Image>
</View>
It seems crazy that there is still not a standard way of anchoring an image in React Native as there is in regular css (at least if there is, I stall haven't found it). But this lib does the trick for me:
react-native-scalable-image
Hope it helps!
If you can get the height of the image h, then you can show the full image first then hide the bottom part with a view container of static height eg. 250:
<View style={{ height: 250 }}>
<Image source={{ uri: source }} style={{ width: null, height: h }} />
</View>
I have found a trick to do that
You need to wrap the wanted height in a View with a overflow hidden
And set a large height in the image style
const WANTED_HEIGHT = 170
<View style={{ height: WANTED_HEIGHT, overflow: 'hidden' }}>
<Image
source={{ uri }}
style={{
width: '100%',
height: 500,
}}
/>
</View>
return (
<View
style={{
flex: 1,
height: 120,
justifyContent: "center",
alignContent: "center"
}}
>
<TouchableOpacity onPress={() => alert("Hello ")}>
<Text style={{ alignSelf: "center" }}>Anchor Tag</Text>
</TouchableOpacity>
<Image
source={{
uri: "https://shoutem.github.io/img/ui-toolkit/examples/image-3.png"
}}
style={{
width: "100%",
height: 100,
resizeMode: "cover"
}}
/>
</View>