Vertically anchor an image to its top in React Native - 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>

Related

React Native Flex box, how to make middle view take all available space, given start and end items have fixed width

I'm reading the Flex layout in React Native but I'm still unable to make the Middle (white box) to take what ever available left (without fixed width, since different device will have different width). Also struggling to make the green box always go to the end.
How would I layout this?
This can be solve by adding:
flexGrow:1
you can take references:
https://demos.scotch.io/visual-guide-to-css3-flexbox-flexbox-playground/demos/
You can do that by using flex
<View style={{flexDirection: 'row', height: 300, alignItems: 'center'}}>
<View style={{width: 40, backgroundColor: 'blue'}} />
<View style={{flex: 1, backgroundColor: 'white'}} />
<View style={{width: 30, backgroundColor: 'green'}} />
</View>
<View style={{flexDirection: 'row', width: '100%', height: 100}}>
<View style={{backgroundColor: 'blue', width: 40}}></View>
<View style={{backgroundColor: 'white', flexGrow: 1}}></View>
<View style={{backgroundColor: 'green', width: 30}}></View>
</View>
Setting flexGrow: 1 works for me.

Am I doing something wrong in my View that the flexDirection doesn't work?

I'm trying to get the text to display to the right of the image, but for some reason flexDirection isn't working the way I am using it.
I've moved the flexDirection property around. Also have tried stuff other than View.
<View
style={{ flexDirection: 'row' }}
style={{
flex: 1,
backgroundColor: this.props.index % 2 == 0 ? '#420225' : '#69053e'
}}
>
<Image
source={{ uri: this.props.item.imageUrl }}
style={{ width: 100, height: 100, margin: 5 }}
/>
<Text style={{ color: 'white' }}>{this.props.item.textContent}
</Text>
</View>
I'm trying to get the Text to display to the right of the Image.
I change your code a little. You should not have 2 styles for your View.
<View
style={{
flex: 1,
flexDirection: 'row' ,
}}>
<Image
source={{ uri: "https://url.com" }}
style={{ width: 100, height: 100, margin: 5 }}
/>
<Text>something
</Text>
</View>

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>

Image borderRadius is not working in react native

I have used borderRadius in Image and loop it. It works in some images however others are rectangular. If I touch it then it shows the radius and disappears as soon as it is untouched. Using overflow hidden doesn't do the trick as well.
I'm confused I used the same style but the result is different. I've tested it on Android devices only.
https://snack.expo.io/#codebyte99/multiplearrays
code:
<TouchableOpacity activeOpacity={0.8}>
<ImageBackground
source={{
uri: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREX0q18KDbtN-obe1EFxAwNg27xgR_KItZ7U8MkXnH7zBCEr_ASQ",
}}
style={[
{
width: 200,
height: 80,
resizeMode: "center",
justifyContent: "flex-end",
alignItems: "center",
margin: 5,
marginRight: 0,
marginTop: 0,
marginBottom: 5,
borderRadius: 6,
overflow: "hidden",
},
]}
>
<Text>{childItem.title}</Text>
</ImageBackground>
</TouchableOpacity>;
You have to set the borderRadius to Image, not in style:
<Image source={{...}} borderRadius={6} .../>
And you don't set overflow: 'hidden' in image style, but in the View:
<View style={{ ..., overflow: 'hidden' }}>
<Image ..../>
</View>
Wrap the Image in a View with overflow: "hidden" and apply borderRadius to that, e.g.
<View
style={{
width: 30,
height: 30,
borderRadius: 15,
overflow: "hidden",
}}
>
<Image
style={{ width: 30, height: 30}}
source={{ uri }}
></Image>
</View>
(I could not find a way to get borderRadius to work on the image directly on Android.)
Applying the same fixed height and width to the image and borderRadius as half of the (height/width) will solve the border issue for android in react-native. Note: Make sure you have resizeMode as 'cover'
<Image key={index} source={{ 'uri' }} resizeMode='cover' style={{ height: 40, width: 40, borderRadius: 20 }} />

React native Text Input with Left and Right View mode

I am trying to build a Search component with Textfield with both right view and left view(Images at both end). Text Input wraps itself to the length of the content, so component no covering entire parent width. How to stretch the component so that entire component occupies the full width of parent.
<View
style={{
flexDirection: "row",
height: 40,
alignSelf: "stretch",
backgroundColor: "red"
}}
>
<Image
source={require("./image_assets_rn/search_rn.png")}
style={{ alignSelf: "center" }}
/>
<TextInput
placeholder="Search Jet"
style={{ backgroundColor: "white", alignSelf: "stretch" }}
/>
<Image
source={require("./image_assets_rn/search_rn.png")}
style={{ alignSelf: "center" }}
/>
</View>;
Below is the output. I want to stretch the textfield along the main axis(width)
It can be achieved giving the TextInput flex:1, there is no need to use stretch.
<View style={styles.container}>
<View style={{flexDirection:'row', width: 300, height:40,backgroundColor:'lightgray'}}>
<Image
source={require('./image_assets_rn/search_rn.png')}
style={{ height: 30, width: 20,margin: 5}}
/>
<TextInput
placeholder="Search Jet"
style={{backgroundColor:'white', flex:1}}
/>
<Image
source={require('./image_assets_rn/search_rn.png')}
style={{ height: 30, width: 20, margin:5}}
/>
</View>
</View>
Check it on snack
I recommend you this tutorial to have a grasp on how flexbox works.
Hope it helps!
I just add component flexbox in each component that wrapped in component View. I think it will make every component follow the parent width.
<View style={{ flexDirection:'row',
height:40,
alignSelf:'stretch',
backgroundColor:'red' }}>
<Image source={ require('./image_assets_rn/search_rn.png')}
style={{ flex:1, alignSelf:'center' }}/>
<TextInput placeholder="Search Jet"
style={{ flex:1, backgroundColor:'white',
alignSelf:'stretch' }}/>
<Image source={ require('./image_assets_rn/search_rn.png')}
style={{ flex:1, alignSelf:'center' }}/>
</View>
If you want to learn more about Flexbox maybe you can read on the Official Web ReactNative.
I hope this can help you :))